Skip to content

Commit cdcab4c

Browse files
Struggling to get gtest to work
it refuses to work on windows.
1 parent 8f0446f commit cdcab4c

File tree

2 files changed

+88
-125
lines changed

2 files changed

+88
-125
lines changed

Stochastic/Tests/statstest.cpp

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <iostream>
88
#include <cstdio>
99

10+
#include "VCELL/SimulationMessaging.h"
11+
1012
const char* input_file_contents = R"INPUT_FILE(
1113
<control>
1214
STARTING_TIME 0.0
@@ -98,70 +100,68 @@ const std::map<int, double> expected_S0 = {
98100

99101
TEST(statstest,test1) {
100102
std::cout << "Checkpoint 0" << std::endl;
101-
char *temp_input_file_name = new char[1000] {0};
102-
char *temp_output_file_name = new char[1000] {0};
103-
std::string in_prefix = testing::TempDir() + "input_XXXXXX";
104-
std::string out_prefix = testing::TempDir() + "output_XXXXXX";
105-
strncpy(temp_input_file_name, in_prefix.c_str(), in_prefix.length());
106-
strncpy(temp_output_file_name, out_prefix.c_str(), out_prefix.length());
103+
//std::string inputFileName = std::tmpnam(nullptr);
104+
std::string inputFileName {R"(C:\CustomTemp\testme_vcell.txt)"};
105+
std::string outputFileName = std::tmpnam(nullptr);
106+
std::map<int,double> results;
107+
std::fstream inputFileStream{inputFileName};
108+
ASSERT_TRUE(inputFileStream.fail());
109+
std::cout << std::system_category().message(::GetLastError()) << std::endl;
110+
std::cout << "Supposed file: " << inputFileName << std::endl;
111+
return;
112+
std::fstream outputFileStream{outputFileName};
113+
ASSERT_FALSE(outputFileStream.fail());
114+
115+
// Setup the Gibson Solver input file
107116
std::cout << "Checkpoint 1" << std::endl;
108-
assert(mkstemp(temp_input_file_name) != -1);
109-
assert(mkstemp(temp_output_file_name) != -1);
110-
std::ofstream input_file (temp_input_file_name);
111-
bool bWroteFile = false;
112-
if (input_file.is_open()){
113-
input_file << input_file_contents;
114-
input_file.close();
115-
bWroteFile = true;
116-
}
117-
ASSERT_TRUE(bWroteFile);
117+
std::cout << " >> " << "Input: <" << inputFileName << ">;"<< std::endl;
118+
std::cout << " >> " << "Output: <" << outputFileName << ">;"<< std::endl;
119+
if (outputFileStream.is_open()) outputFileStream.close();
120+
inputFileStream << input_file_contents;
121+
inputFileStream.close();
122+
return;
123+
124+
// Create the Gibson Solver
118125
std::cout << "Checkpoint 2" << std::endl;
119-
Gibson *gb= new Gibson(temp_input_file_name, temp_output_file_name);
126+
auto *gb = new Gibson(inputFileName.c_str(), outputFileName.c_str());
127+
128+
// Launch the test
120129
std::cout << "Checkpoint 3" << std::endl;
121130
gb->march();
122131

132+
// Verify file contents
123133
std::cout << "Checkpoint 4" << std::endl;
124-
// verify file contents
125-
std::ifstream outfile(temp_output_file_name);
126-
string line;
127-
getline(outfile, line); // remove header line
128-
// std::cout << line << std::endl;
129-
// std::cout.flush();
130-
134+
outputFileStream.open(outputFileName);
135+
std::string line;
136+
std::getline(outputFileStream, line); // remove header line
137+
for (int i = 0; !outputFileStream.eof(); i++) {
138+
std::getline(outputFileStream, line);
139+
// if index found in expected_S0 map, store in results map
140+
if (expected_S0.find(i) != expected_S0.end()){
141+
float t, s0, s1, s2;
142+
// extract space separated values for t, s0, s1 and s2 from line
143+
std::stringstream line_stream(line);
144+
line_stream >> t >> s0 >> s1 >> s2;
145+
results[i] = s0;
146+
}
147+
}
148+
outputFileStream.close();
149+
150+
// compare the expected and actual values
131151
std::cout << "Checkpoint 5" << std::endl;
132-
std::map<int,double> results;
133-
int i = 0;
134-
while (getline(outfile, line)){
135-
// if index found in expected_S0 map, store in results map
136-
if (expected_S0.find(i) != expected_S0.end()){
137-
float t, s0, s1, s2;
138-
// extract space separated values for t, s0, s1 and s2 from line
139-
std::stringstream line_stream(line);
140-
line_stream >> t >> s0 >> s1 >> s2;
141-
results[i] = s0;
142-
// std::cout << line << std::endl;
143-
// std::cout.flush();
144-
}
145-
i++;
146-
}
147-
std::cout << "Checkpoint 6" << std::endl;
148-
// compare the expected and actual values
149-
double accum_error = 0.0;
150-
double max_error = 0.0;
152+
double accumulatedError = 0.0, maxIndividualError = 0.0;
151153
for (auto const& expected : expected_S0){
152-
double s0_given = expected.second;
153-
double s0_computed = results[expected.first];
154-
double abserr = std::abs(s0_given - s0_computed);
155-
// std::cout << "t=" << expected.first << " expected=" << s0_given << " computed=" << s0_computed << " abserr=" << abserr << std::endl;
156-
accum_error += abserr;
157-
max_error = std::max(max_error, abserr);
154+
double absoluteError = std::abs(expected.second - results[expected.first]);
155+
// std::cout << "t=" << expected.first << " expected=" << expected.second << " computed=" << results[expected.first] << " abserr=" << abserr << std::endl;
156+
accumulatedError += absoluteError;
157+
maxIndividualError = std::max(maxIndividualError, absoluteError);
158158
}
159-
assert(accum_error < 0.015);
160-
assert(max_error < 0.005);
159+
assert(accumulatedError < 0.015);
160+
assert(maxIndividualError < 0.005);
161161
std::cout << "Checkpoint 7" << std::endl;
162162

163163
delete gb;
164-
delete[] temp_input_file_name;
165-
delete[] temp_output_file_name;
164+
if (inputFileStream.is_open()) inputFileStream.close();
165+
if (outputFileStream.is_open()) outputFileStream.close();
166166
std::cout << "Checkpoint 8" << std::endl;
167167
}

Stochastic/VCellStoch/src/Gibson.cpp

Lines changed: 35 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
#include <string>
1111
#include <iomanip>
1212
#include <vector>
13-
#include <stdint.h>
14-
#include <math.h>
15-
#include <stdlib.h>
13+
#include <cstdint>
14+
#include <cmath>
15+
#include <cstdlib>
1616
#include <random>
1717
using namespace std;
1818

19-
#include <time.h>
19+
#include <ctime>
2020
#include "../include/IndexedTree.h"
2121

2222
#ifdef USE_MESSAGING
@@ -26,14 +26,12 @@ using namespace std;
2626
const double double_infinity = numeric_limits<double>::infinity();
2727
const double EPSILON = 1E-12;
2828
const string Gibson::MY_T_STR = "t";
29+
2930
/*
3031
*Empty constructor of Gibson. It will use the defalt settings in StochModel.
3132
*/
3233
Gibson::Gibson()
33-
:StochModel(),
34-
savedSampleCount(1),
35-
lastTime (std::numeric_limits<long>::min( ))
36-
{
34+
: savedSampleCount(1), lastTime (std::numeric_limits<long>::min()) {
3735
Tree=NULL;
3836
currvals=NULL;
3937
generator = new std::mt19937_64();
@@ -47,16 +45,13 @@ Gibson::Gibson()
4745
* string, the output file(name), where the results are saved.
4846
*/
4947
Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename)
50-
:StochModel(),
51-
savedSampleCount(1),
52-
lastTime (std::numeric_limits<long>::min( ))
53-
{
54-
Tree=NULL;
55-
currvals=NULL;
56-
generator = new std::mt19937_64();
57-
distribution = new std::uniform_real_distribution<double>(0.0,1.0);
58-
59-
outfilename=arg_outfilename;
48+
: savedSampleCount(1), lastTime (std::numeric_limits<long>::min( )) {
49+
// TODO: Call basic constructor rather than duplicate lines
50+
this->Tree = NULL;
51+
this->currvals = NULL;
52+
this->generator = new std::mt19937_64();
53+
this->distribution = new std::uniform_real_distribution<>(0.0,1.0);
54+
this->outfilename = arg_outfilename;
6055

6156
ifstream infile;
6257
string instring;
@@ -69,67 +64,41 @@ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename)
6964
while(infile >> instring)
7065
{
7166
//load control info.
72-
if (instring == "STARTING_TIME")
73-
{
67+
if (instring == "STARTING_TIME"){
7468
infile >> STARTING_TIME;
75-
}
76-
else if (instring == "BMULTIBUTNOTHISTO")
77-
{
69+
} else if (instring == "BMULTIBUTNOTHISTO"){
7870
infile >> bMultiButNotHisto;
79-
}
80-
else if (instring == "ENDING_TIME")
81-
{
71+
} else if (instring == "ENDING_TIME"){
8272
infile >> ENDING_TIME;
83-
}
84-
else if (instring == "SAVE_PERIOD")
85-
{
73+
} else if (instring == "SAVE_PERIOD"){
8674
infile >> SAVE_PERIOD;
8775
flag_savePeriod=true;
88-
}
89-
else if (instring == "MAX_ITERATION")
90-
{
76+
} else if (instring == "MAX_ITERATION"){
9177
infile >> MAX_ITERATION;
92-
}
93-
else if (instring == "TOLERANCE")
94-
{
78+
} else if (instring == "TOLERANCE"){
9579
infile >> TOLERANCE;
96-
}
97-
else if (instring == "SAMPLE_INTERVAL")
98-
{
80+
} else if (instring == "SAMPLE_INTERVAL"){
9981
infile >> SAMPLE_INTERVAL;
100-
}
101-
else if (instring == "MAX_SAVE_POINTS")
102-
{
82+
} else if (instring == "MAX_SAVE_POINTS"){
10383
infile >> MAX_SAVE_POINTS;
104-
}
105-
else if (instring == "NUM_TRIAL")
106-
{
84+
} else if (instring == "NUM_TRIAL"){
10785
infile >> NUM_TRIAL;
108-
}
109-
else if (instring == "SEED")
110-
{
86+
} else if (instring == "SEED"){
11187
infile >> SEED;
112-
}
113-
//load listofvars
114-
else if (instring == "TotalVars")
115-
{
88+
} else if (instring == "TotalVars"){ //load listofvars
11689
int varCount;
11790
infile >> varCount;
11891
string name;
11992
double amount;
120-
for(int i=0;i<varCount;i++)
121-
{
93+
for(int i=0;i<varCount;i++){
12294
infile >> name;
12395
infile >> amount;
12496
listOfVarNames.push_back(name);
12597
listOfIniValues.push_back((uint64_t)amount);
12698
StochVar *var=new StochVar((uint64_t)amount);
12799
listOfVars.push_back(var);
128100
}
129-
}
130-
//load listOfProcesses
131-
else if (instring == "TotalProcesses")
132-
{
101+
} else if (instring == "TotalProcesses"){ //load listOfProcesses
133102
int pCount;
134103
infile >> pCount;
135104
string name;
@@ -140,15 +109,11 @@ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename)
140109
Jump *jp=new Jump();
141110
listOfProcesses.push_back(jp);
142111
}
143-
}
144-
//load process description to set up processes
145-
else if (instring == "TotalDescriptions")
146-
{
112+
} else if (instring == "TotalDescriptions") { //load process description to set up processes
147113
int dCount,idx;
148114
infile >> dCount;
149115
string name,str;
150-
for(int i=0;i<dCount;i++)//loop through each process description
151-
{
116+
for(int i=0;i<dCount;i++){ //loop through each process description
152117
infile >> name >> name;// "process name"
153118
//find the process in listOfProcesses using it's name
154119
idx=getProcessIndex(name);
@@ -201,10 +166,9 @@ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename)
201166
}
202167
}
203168
//setup IndexedTree
204-
Tree=new IndexedTree();
205-
for(int i=0;i<listOfProcesses.size();i++)
206-
{
207-
Tree->addProcess(listOfProcesses[i]);
169+
Tree = new IndexedTree();
170+
for(auto & listOfProcesse : listOfProcesses){
171+
Tree->addProcess(listOfProcesse);
208172
}
209173
infile.close();
210174
if (NUM_TRIAL > MAX_ALLOWED_POINTS) {
@@ -214,7 +178,6 @@ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename)
214178
VCELL_EXCEPTION(invalid_argument,"Stochastic initialization: Server save points " << MAX_SAVE_POINTS << " exceeds limit of " << MAX_ALLOWED_POINTS);
215179
}
216180

217-
218181
if(NUM_TRIAL > 1 && !bMultiButNotHisto){
219182
//this must be a gibson 'histogram' sim,
220183
//java gui not allow setting of MAX_SAVE_POINTS and default is too low
@@ -270,12 +233,12 @@ Gibson::~Gibson()
270233
//delete indexedTree
271234
delete Tree;
272235
//delete variables
273-
for(int i=0;i<listOfVars.size();i++)
274-
delete listOfVars[i];
236+
for(auto & listOfVar : listOfVars)
237+
delete listOfVar;
275238
listOfVars.clear();
276239
//delete proccesses
277-
for(int i=0;i<listOfProcesses.size();i++)
278-
delete listOfProcesses[i];
240+
for(auto & listOfProcesse : listOfProcesses)
241+
delete listOfProcesse;
279242
listOfProcesses.clear();
280243
//delete vectors
281244
listOfVarNames.clear();

0 commit comments

Comments
 (0)