Skip to content

Commit dd26e18

Browse files
ischoeglspeth
authored andcommitted
[googletest] Simplify char* handling
1 parent a12f8f7 commit dd26e18

File tree

4 files changed

+45
-60
lines changed

4 files changed

+45
-60
lines changed

test/clib/test_clib.cpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ using ::testing::HasSubstr;
1010

1111
string reportError()
1212
{
13-
int buflen = 0;
14-
char* output_buf = 0;
15-
buflen = ct_getCanteraError(buflen, output_buf) + 1;
16-
output_buf = new char[buflen];
17-
ct_getCanteraError(buflen, output_buf);
18-
string err = output_buf;
19-
delete[] output_buf;
20-
return err;
13+
vector<char> output_buf;
14+
int buflen = ct_getCanteraError(0, output_buf.data()) + 1;
15+
output_buf.resize(buflen);
16+
ct_getCanteraError(buflen, output_buf.data());
17+
return string(output_buf.data());
2118
}
2219

2320
TEST(ct, cabinet_exceptions)
@@ -62,11 +59,10 @@ TEST(ct, new_solution)
6259
int thermo = soln_thermo(ref);
6360
ASSERT_EQ(thermo_parent(thermo), ref);
6461

65-
char* buf = new char[buflen];
66-
soln_name(ref, buflen, buf);
67-
string solName = buf;
62+
vector<char> buf(buflen);
63+
soln_name(ref, buflen, buf.data());
64+
string solName(buf.data());
6865
ASSERT_EQ(solName, name);
69-
delete[] buf;
7066
}
7167

7268
TEST(ct, soln_objects)
@@ -142,19 +138,17 @@ TEST(ct, new_interface)
142138

143139
int ph_surf = soln_thermo(surf);
144140
int buflen = soln_name(ph_surf, 0, 0) + 1; // include \0
145-
char* buf = new char[buflen];
146-
soln_name(ph_surf, buflen, buf);
147-
string solName = buf;
141+
vector<char> buf(buflen);
142+
soln_name(ph_surf, buflen, buf.data());
143+
string solName(buf.data());
148144
ASSERT_EQ(solName, "Pt_surf");
149-
delete[] buf;
150145

151146
int kin_surf = soln_kinetics(surf);
152147
buflen = kin_getType(kin_surf, 0, 0) + 1; // include \0
153-
buf = new char[buflen];
154-
kin_getType(ph_surf, buflen, buf);
155-
string kinType = buf;
148+
buf.resize(buflen);
149+
kin_getType(ph_surf, buflen, buf.data());
150+
string kinType(buf.data());
156151
ASSERT_EQ(kinType, "surface");
157-
delete[] buf;
158152
}
159153

160154
TEST(ct, new_interface_auto)
@@ -170,18 +164,16 @@ TEST(ct, new_interface_auto)
170164
ASSERT_EQ(gas, 1);
171165

172166
int buflen = soln_name(gas, 0, 0) + 1; // include \0
173-
char* buf = new char[buflen];
174-
soln_name(gas, buflen, buf);
175-
string solName = buf;
167+
vector<char> buf(buflen);
168+
soln_name(gas, buflen, buf.data());
169+
string solName(buf.data());
176170
ASSERT_EQ(solName, "gas");
177-
delete[] buf;
178171

179172
buflen = soln_adjacentName(surf, 0, 0, 0) + 1;
180-
char* buf2 = new char[buflen];
181-
soln_adjacentName(surf, 0, buflen, buf2);
182-
solName = buf2;
173+
buf.resize(buflen);
174+
soln_adjacentName(surf, 0, buflen, buf.data());
175+
solName = buf.data();
183176
ASSERT_EQ(solName, "gas");
184-
delete[] buf2;
185177
}
186178

187179
TEST(ct, thermo)

test/clib/test_ctfunc.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ TEST(ctfunc, sin)
2727
EXPECT_DOUBLE_EQ(func_value(dfcn, 0.5), omega * cos(omega * 0.5));
2828

2929
int buflen = func_write(fcn, "x", 0, 0);
30-
char* buf = new char[buflen];
31-
func_write(fcn, "x", buflen, buf);
32-
string rep = buf;
30+
vector<char> buf(buflen);
31+
func_write(fcn, "x", buflen, buf.data());
32+
string rep(buf.data());
3333
ASSERT_EQ(rep, "\\sin(2.1x)");
34-
delete[] buf;
3534
}
3635

3736
TEST(ctfunc, cos)
@@ -123,11 +122,10 @@ TEST(ctfunc, poly)
123122
params = {1, 0, -2.2, 3.1};
124123
fcn = func_new_advanced("polynomial3", params.size(), params.data());
125124
int buflen = func_write(fcn, "x", 0, 0);
126-
char* buf = new char[buflen];
127-
func_write(fcn, "x", buflen, buf);
128-
string rep = buf;
125+
vector<char> buf(buflen);
126+
func_write(fcn, "x", buflen, buf.data());
127+
string rep(buf.data());
129128
ASSERT_EQ(rep, "x^3 - 2.2x + 3.1");
130-
delete[] buf;
131129
}
132130

133131
TEST(ctfunc, Fourier)

test/clib/test_ctonedim.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ TEST(ctonedim, freeflow)
2929
ASSERT_NEAR(flow1D_pressure(flow), P, 1e-5);
3030

3131
int buflen = domain_type(flow, 0, 0);
32-
char* buf = new char[buflen];
33-
domain_type(flow, buflen, buf);
34-
string domType = buf;
32+
vector<char> buf(buflen);
33+
domain_type(flow, buflen, buf.data());
34+
string domType(buf.data());
3535
ASSERT_EQ(domType, "free-flow");
36-
delete[] buf;
3736
}
3837

3938
TEST(ctonedim, inlet)
@@ -43,11 +42,10 @@ TEST(ctonedim, inlet)
4342
ASSERT_GE(inlet, 0);
4443

4544
int buflen = domain_type(inlet, 0, 0);
46-
char* buf = new char[buflen];
47-
domain_type(inlet, buflen, buf);
48-
string domType = buf;
45+
vector<char> buf(buflen);
46+
domain_type(inlet, buflen, buf.data());
47+
string domType(buf.data());
4948
ASSERT_EQ(domType, "inlet");
50-
delete[] buf;
5149
}
5250

5351
TEST(ctonedim, outlet)
@@ -57,11 +55,10 @@ TEST(ctonedim, outlet)
5755
ASSERT_GE(outlet, 0);
5856

5957
int buflen = domain_type(outlet, 0, 0);
60-
char* buf = new char[buflen];
61-
domain_type(outlet, buflen, buf);
62-
string domType = buf;
58+
vector<char> buf(buflen);
59+
domain_type(outlet, buflen, buf.data());
60+
string domType(buf.data());
6361
ASSERT_EQ(domType, "outlet");
64-
delete[] buf;
6562
}
6663

6764
TEST(ctonedim, reacting_surface)
@@ -71,11 +68,10 @@ TEST(ctonedim, reacting_surface)
7168
ASSERT_GE(surf, 0);
7269

7370
int buflen = domain_type(surf, 0, 0);
74-
char* buf = new char[buflen];
75-
domain_type(surf, buflen, buf);
76-
string domType = buf;
71+
vector<char> buf(buflen);
72+
domain_type(surf, buflen, buf.data());
73+
string domType(buf.data());
7774
ASSERT_EQ(domType, "reacting-surface");
78-
delete[] buf;
7975
}
8076

8177
TEST(ctonedim, catcomb)
@@ -170,11 +166,11 @@ TEST(ctonedim, freeflame)
170166
for (size_t i = 0; i < nsp; i++) {
171167
value = {yin[i], yin[i], yout[i], yout[i]};
172168
int buflen = thermo_getSpeciesName(ph, i, 0, 0) + 1; // include \0
173-
char* buf = new char[buflen];
174-
thermo_getSpeciesName(ph, i, buflen, buf);
175-
string name = buf;
169+
vector<char> buf(buflen);
170+
thermo_getSpeciesName(ph, i, buflen, buf.data());
171+
string name(buf.data());
176172
ASSERT_EQ(name, gas->speciesName(i));
177-
comp = static_cast<int>(domain_componentIndex(flow, buf));
173+
comp = static_cast<int>(domain_componentIndex(flow, buf.data()));
178174
sim1D_setProfile(flame, dom, comp, 4, locs.data(), 4, value.data());
179175
}
180176

test/clib/test_ctreactor.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ TEST(ctreactor, reactor_soln)
1717
int ret = reactor_setName(reactor, "spam");
1818
ASSERT_EQ(ret, 0);
1919
int buflen = reactor_name(reactor, 0, 0);
20-
char* buf = new char[buflen];
21-
reactor_name(reactor, buflen, buf);
22-
string rName = buf;
20+
vector<char> buf(buflen);
21+
reactor_name(reactor, buflen, buf.data());
22+
string rName(buf.data());
2323
ASSERT_EQ(rName, "spam");
24-
delete[] buf;
2524
}
2625

2726
vector<double> T_ctreactor = {

0 commit comments

Comments
 (0)