Skip to content

Commit

Permalink
[googletest] Simplify char* handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Aug 24, 2024
1 parent a12f8f7 commit dd26e18
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 60 deletions.
48 changes: 20 additions & 28 deletions test/clib/test_clib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ using ::testing::HasSubstr;

string reportError()
{
int buflen = 0;
char* output_buf = 0;
buflen = ct_getCanteraError(buflen, output_buf) + 1;
output_buf = new char[buflen];
ct_getCanteraError(buflen, output_buf);
string err = output_buf;
delete[] output_buf;
return err;
vector<char> output_buf;
int buflen = ct_getCanteraError(0, output_buf.data()) + 1;
output_buf.resize(buflen);
ct_getCanteraError(buflen, output_buf.data());
return string(output_buf.data());
}

TEST(ct, cabinet_exceptions)
Expand Down Expand Up @@ -62,11 +59,10 @@ TEST(ct, new_solution)
int thermo = soln_thermo(ref);
ASSERT_EQ(thermo_parent(thermo), ref);

char* buf = new char[buflen];
soln_name(ref, buflen, buf);
string solName = buf;
vector<char> buf(buflen);
soln_name(ref, buflen, buf.data());
string solName(buf.data());
ASSERT_EQ(solName, name);
delete[] buf;
}

TEST(ct, soln_objects)
Expand Down Expand Up @@ -142,19 +138,17 @@ TEST(ct, new_interface)

int ph_surf = soln_thermo(surf);
int buflen = soln_name(ph_surf, 0, 0) + 1; // include \0
char* buf = new char[buflen];
soln_name(ph_surf, buflen, buf);
string solName = buf;
vector<char> buf(buflen);
soln_name(ph_surf, buflen, buf.data());
string solName(buf.data());
ASSERT_EQ(solName, "Pt_surf");
delete[] buf;

int kin_surf = soln_kinetics(surf);
buflen = kin_getType(kin_surf, 0, 0) + 1; // include \0
buf = new char[buflen];
kin_getType(ph_surf, buflen, buf);
string kinType = buf;
buf.resize(buflen);
kin_getType(ph_surf, buflen, buf.data());
string kinType(buf.data());
ASSERT_EQ(kinType, "surface");
delete[] buf;
}

TEST(ct, new_interface_auto)
Expand All @@ -170,18 +164,16 @@ TEST(ct, new_interface_auto)
ASSERT_EQ(gas, 1);

int buflen = soln_name(gas, 0, 0) + 1; // include \0
char* buf = new char[buflen];
soln_name(gas, buflen, buf);
string solName = buf;
vector<char> buf(buflen);
soln_name(gas, buflen, buf.data());
string solName(buf.data());
ASSERT_EQ(solName, "gas");
delete[] buf;

buflen = soln_adjacentName(surf, 0, 0, 0) + 1;
char* buf2 = new char[buflen];
soln_adjacentName(surf, 0, buflen, buf2);
solName = buf2;
buf.resize(buflen);
soln_adjacentName(surf, 0, buflen, buf.data());
solName = buf.data();
ASSERT_EQ(solName, "gas");
delete[] buf2;
}

TEST(ct, thermo)
Expand Down
14 changes: 6 additions & 8 deletions test/clib/test_ctfunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ TEST(ctfunc, sin)
EXPECT_DOUBLE_EQ(func_value(dfcn, 0.5), omega * cos(omega * 0.5));

int buflen = func_write(fcn, "x", 0, 0);
char* buf = new char[buflen];
func_write(fcn, "x", buflen, buf);
string rep = buf;
vector<char> buf(buflen);
func_write(fcn, "x", buflen, buf.data());
string rep(buf.data());
ASSERT_EQ(rep, "\\sin(2.1x)");
delete[] buf;
}

TEST(ctfunc, cos)
Expand Down Expand Up @@ -123,11 +122,10 @@ TEST(ctfunc, poly)
params = {1, 0, -2.2, 3.1};
fcn = func_new_advanced("polynomial3", params.size(), params.data());
int buflen = func_write(fcn, "x", 0, 0);
char* buf = new char[buflen];
func_write(fcn, "x", buflen, buf);
string rep = buf;
vector<char> buf(buflen);
func_write(fcn, "x", buflen, buf.data());
string rep(buf.data());
ASSERT_EQ(rep, "x^3 - 2.2x + 3.1");
delete[] buf;
}

TEST(ctfunc, Fourier)
Expand Down
36 changes: 16 additions & 20 deletions test/clib/test_ctonedim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ TEST(ctonedim, freeflow)
ASSERT_NEAR(flow1D_pressure(flow), P, 1e-5);

int buflen = domain_type(flow, 0, 0);
char* buf = new char[buflen];
domain_type(flow, buflen, buf);
string domType = buf;
vector<char> buf(buflen);
domain_type(flow, buflen, buf.data());
string domType(buf.data());
ASSERT_EQ(domType, "free-flow");
delete[] buf;
}

TEST(ctonedim, inlet)
Expand All @@ -43,11 +42,10 @@ TEST(ctonedim, inlet)
ASSERT_GE(inlet, 0);

int buflen = domain_type(inlet, 0, 0);
char* buf = new char[buflen];
domain_type(inlet, buflen, buf);
string domType = buf;
vector<char> buf(buflen);
domain_type(inlet, buflen, buf.data());
string domType(buf.data());
ASSERT_EQ(domType, "inlet");
delete[] buf;
}

TEST(ctonedim, outlet)
Expand All @@ -57,11 +55,10 @@ TEST(ctonedim, outlet)
ASSERT_GE(outlet, 0);

int buflen = domain_type(outlet, 0, 0);
char* buf = new char[buflen];
domain_type(outlet, buflen, buf);
string domType = buf;
vector<char> buf(buflen);
domain_type(outlet, buflen, buf.data());
string domType(buf.data());
ASSERT_EQ(domType, "outlet");
delete[] buf;
}

TEST(ctonedim, reacting_surface)
Expand All @@ -71,11 +68,10 @@ TEST(ctonedim, reacting_surface)
ASSERT_GE(surf, 0);

int buflen = domain_type(surf, 0, 0);
char* buf = new char[buflen];
domain_type(surf, buflen, buf);
string domType = buf;
vector<char> buf(buflen);
domain_type(surf, buflen, buf.data());
string domType(buf.data());
ASSERT_EQ(domType, "reacting-surface");
delete[] buf;
}

TEST(ctonedim, catcomb)
Expand Down Expand Up @@ -170,11 +166,11 @@ TEST(ctonedim, freeflame)
for (size_t i = 0; i < nsp; i++) {
value = {yin[i], yin[i], yout[i], yout[i]};
int buflen = thermo_getSpeciesName(ph, i, 0, 0) + 1; // include \0
char* buf = new char[buflen];
thermo_getSpeciesName(ph, i, buflen, buf);
string name = buf;
vector<char> buf(buflen);
thermo_getSpeciesName(ph, i, buflen, buf.data());
string name(buf.data());
ASSERT_EQ(name, gas->speciesName(i));
comp = static_cast<int>(domain_componentIndex(flow, buf));
comp = static_cast<int>(domain_componentIndex(flow, buf.data()));
sim1D_setProfile(flame, dom, comp, 4, locs.data(), 4, value.data());
}

Expand Down
7 changes: 3 additions & 4 deletions test/clib/test_ctreactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ TEST(ctreactor, reactor_soln)
int ret = reactor_setName(reactor, "spam");
ASSERT_EQ(ret, 0);
int buflen = reactor_name(reactor, 0, 0);
char* buf = new char[buflen];
reactor_name(reactor, buflen, buf);
string rName = buf;
vector<char> buf(buflen);
reactor_name(reactor, buflen, buf.data());
string rName(buf.data());
ASSERT_EQ(rName, "spam");
delete[] buf;
}

vector<double> T_ctreactor = {
Expand Down

0 comments on commit dd26e18

Please sign in to comment.