Skip to content

Commit cbb3b7f

Browse files
authored
Merge pull request #198 from usnistgov/BUGFIX/DuplicateOutput
Bugfix/duplicate output
2 parents 219d232 + b3ee04d commit cbb3b7f

File tree

6 files changed

+272
-99
lines changed

6 files changed

+272
-99
lines changed

cpp/conditioning_main.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,14 @@ int main(int argc, char* argv[]) {
446446
string timestamp = getCurrentTimestamp();
447447
string outputfilename;
448448
string inputfilename;
449+
string commandline = recreateCommandLine(argc, argv);
449450

450-
for(int i = 0; i < argc; i++) {
451+
for (int i = 0; i < argc; i++) {
451452
std::string Str = std::string(argv[i]);
452-
if("--version" == Str) {
453+
if ("--version" == Str) {
453454
printVersion("conditioning");
454455
exit(0);
455-
}
456+
}
456457
}
457458

458459
while ((opt = getopt(argc, argv, "vnqo:i:c:")) != -1) {
@@ -489,6 +490,7 @@ int main(int argc, char* argv[]) {
489490
NonIidTestRun testRunNonIid;
490491
testRunNonIid.type = "Conditioning";
491492
testRunNonIid.timestamp = timestamp;
493+
testRunNonIid.commandline = commandline;
492494

493495
// Parse args
494496
if (vetted && argc != 4) {
@@ -511,7 +513,17 @@ int main(int argc, char* argv[]) {
511513

512514
// get h_in; note that h_in <= n_in
513515
h_in = inputLongDoubleOption(argv[3], 0.0L, (long double) n_in, "h_in");
514-
if (h_in <= 0.0L) print_usage();
516+
if (h_in <= 0.0L) {
517+
if (jsonOutput) {
518+
testRunNonIid.errorLevel = -1;
519+
testRunNonIid.errorMsg = "Error with input: generating h_in.";
520+
ofstream output;
521+
output.open(outputfilename);
522+
output << testRunNonIid.GetAsJson();
523+
output.close();
524+
}
525+
print_usage();
526+
}
515527

516528
if (!vetted) {
517529
if (argc == 4) {
@@ -520,7 +532,17 @@ int main(int argc, char* argv[]) {
520532
} else {
521533
// If h_p is provided via command line, use that value
522534
h_p = inputLongDoubleOption(argv[4], 0.0L, 1.0L, "h_p");
523-
if (h_p <= 0) print_usage();
535+
if (h_p <= 0) {
536+
if (jsonOutput) {
537+
testRunNonIid.errorLevel = -1;
538+
testRunNonIid.errorMsg = "Error with input: generating h_p.";
539+
ofstream output;
540+
output.open(outputfilename);
541+
output << testRunNonIid.GetAsJson();
542+
output.close();
543+
}
544+
print_usage();
545+
}
524546
}
525547
}
526548
}

cpp/iid_main.cpp

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,20 @@ int main(int argc, char* argv[]) {
7575
bool jsonOutput = false;
7676
string timestamp = getCurrentTimestamp();
7777
string outputfilename;
78+
string commandline = recreateCommandLine(argc, argv);
7879

79-
for(int i = 0; i < argc; i++) {
80+
IidTestRun testRun;
81+
testRun.timestamp = timestamp;
82+
testRun.commandline = commandline;
83+
84+
for (int i = 0; i < argc; i++) {
8085
std::string Str = std::string(argv[i]);
81-
if("--version" == Str) {
86+
if ("--version" == Str) {
8287
printVersion("iid");
8388
exit(0);
84-
}
89+
}
8590
}
86-
91+
8792
while ((opt = getopt(argc, argv, "icatvl:qo:")) != -1) {
8893
switch (opt) {
8994
case 'i':
@@ -104,6 +109,16 @@ int main(int argc, char* argv[]) {
104109
case 'l':
105110
inint = strtoull(optarg, &nextOption, 0);
106111
if ((inint > ULONG_MAX) || (errno == EINVAL) || (nextOption == NULL) || (*nextOption != ',')) {
112+
113+
testRun.errorLevel = -1;
114+
testRun.errorMsg = "Error on index/samples.";
115+
116+
if (jsonOutput) {
117+
ofstream output;
118+
output.open(outputfilename);
119+
output << testRun.GetAsJson();
120+
output.close();
121+
}
107122
print_usage();
108123
}
109124
subsetIndex = inint;
@@ -112,6 +127,15 @@ int main(int argc, char* argv[]) {
112127

113128
inint = strtoull(nextOption, NULL, 0);
114129
if ((inint > ULONG_MAX) || (errno == EINVAL)) {
130+
testRun.errorLevel = -1;
131+
testRun.errorMsg = "Error on index/samples.";
132+
133+
if (jsonOutput) {
134+
ofstream output;
135+
output.open(outputfilename);
136+
output << testRun.GetAsJson();
137+
output.close();
138+
}
115139
print_usage();
116140
}
117141

@@ -146,8 +170,6 @@ int main(int argc, char* argv[]) {
146170
// get filename
147171
file_path = argv[0];
148172

149-
IidTestRun testRun;
150-
testRun.timestamp = timestamp;
151173
testRun.filename = file_path;
152174

153175
if (argc == 2) {
@@ -231,19 +253,19 @@ int main(int argc, char* argv[]) {
231253
int sample_size = data.len;
232254

233255
if ((verbose == 1) || (verbose == 2))
234-
printf("Calculating baseline statistics...\n");
235-
256+
printf("Calculating baseline statistics...\n");
257+
236258
calc_stats(&data, rawmean, median);
237259

238-
if(verbose == 2) {
239-
printf("\tRaw Mean: %f\n", rawmean);
240-
printf("\tMedian: %f\n", median);
241-
printf("\tBinary: %s\n\n", (alphabet_size == 2 ? "true" : "false"));
242-
} else if(verbose > 2) {
243-
printf("Raw Mean = %.17g\n", rawmean);
244-
printf("Median = %.17g\n", median);
245-
printf("Binary = %s\n", (alphabet_size == 2 ? "true" : "false"));
246-
}
260+
if (verbose == 2) {
261+
printf("\tRaw Mean: %f\n", rawmean);
262+
printf("\tMedian: %f\n", median);
263+
printf("\tBinary: %s\n\n", (alphabet_size == 2 ? "true" : "false"));
264+
} else if (verbose > 2) {
265+
printf("Raw Mean = %.17g\n", rawmean);
266+
printf("Median = %.17g\n", median);
267+
printf("Binary = %s\n", (alphabet_size == 2 ? "true" : "false"));
268+
}
247269

248270
IidTestCase tc;
249271
tc.mean = rawmean;
@@ -303,7 +325,7 @@ int main(int argc, char* argv[]) {
303325
} else {
304326
printf("** Failed chi square tests\n\n");
305327
}
306-
} else if(verbose > 2) {
328+
} else if (verbose > 2) {
307329
if (chi_square_test_pass) {
308330
printf("Chi square tests: Passed\n");
309331
} else {
@@ -321,7 +343,7 @@ int main(int argc, char* argv[]) {
321343
} else {
322344
printf("** Failed length of longest repeated substring test\n\n");
323345
}
324-
} else if(verbose > 2) {
346+
} else if (verbose > 2) {
325347
if (len_LRS_test_pass) {
326348
printf("Length of longest repeated substring test: Passed\n");
327349
} else {
@@ -338,13 +360,13 @@ int main(int argc, char* argv[]) {
338360
printf("** Passed IID permutation tests\n\n");
339361
} else {
340362
printf("** Failed IID permutation tests\n\n");
341-
}
342-
} else if(verbose > 2) {
363+
}
364+
} else if (verbose > 2) {
343365
if (perm_test_pass) {
344366
printf("IID permutation tests: Passed\n");
345367
} else {
346368
printf("IID permutation tests: Failed\n");
347-
}
369+
}
348370
}
349371

350372
testRun.testCases.push_back(tc);

cpp/non_iid_main.cpp

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,25 @@ int main(int argc, char* argv[]) {
7676
bool jsonOutput = false;
7777
string timestamp = getCurrentTimestamp();
7878
string outputfilename;
79+
string commandline = recreateCommandLine(argc, argv);
80+
81+
NonIidTestRun testRun;
82+
testRun.timestamp = timestamp;
83+
testRun.commandline = commandline;
84+
7985
data.word_size = 0;
8086

8187
initial_entropy = true;
8288
all_bits = true;
8389

84-
for(int i = 0; i < argc; i++) {
90+
for (int i = 0; i < argc; i++) {
8591
std::string Str = std::string(argv[i]);
86-
if("--version" == Str) {
92+
if ("--version" == Str) {
8793
printVersion("noniid");
8894
exit(0);
89-
}
95+
}
9096
}
91-
97+
9298
while ((opt = getopt(argc, argv, "icatvql:o:")) != -1) {
9399
switch (opt) {
94100
case 'i':
@@ -112,6 +118,15 @@ int main(int argc, char* argv[]) {
112118
case 'l':
113119
inint = strtoull(optarg, &nextOption, 0);
114120
if ((inint > ULONG_MAX) || (errno == EINVAL) || (nextOption == NULL) || (*nextOption != ',')) {
121+
testRun.errorLevel = -1;
122+
testRun.errorMsg = "Error on index/samples.";
123+
124+
if (jsonOutput) {
125+
ofstream output;
126+
output.open(outputfilename);
127+
output << testRun.GetAsJson();
128+
output.close();
129+
}
115130
print_usage();
116131
}
117132
subsetIndex = inint;
@@ -120,6 +135,15 @@ int main(int argc, char* argv[]) {
120135

121136
inint = strtoull(nextOption, NULL, 0);
122137
if ((inint > ULONG_MAX) || (errno == EINVAL)) {
138+
testRun.errorLevel = -1;
139+
testRun.errorMsg = "Error on index/samples.";
140+
141+
if (jsonOutput) {
142+
ofstream output;
143+
output.open(outputfilename);
144+
output << testRun.GetAsJson();
145+
output.close();
146+
}
123147
print_usage();
124148
}
125149
subsetSize = inint;
@@ -153,24 +177,33 @@ int main(int argc, char* argv[]) {
153177
char hash[65];
154178
sha256_file(file_path, hash);
155179

156-
NonIidTestRun testRun;
157-
testRun.timestamp = timestamp;
158180
testRun.sha256 = hash;
159181
testRun.filename = file_path;
160182

161183
if (argc == 2) {
162184
// get bits per word
163185
inint = atoi(argv[1]);
164186
if (inint < 1 || inint > 8) {
187+
188+
testRun.errorLevel = -1;
189+
testRun.errorMsg = "Invalid bits per symbol.";
190+
191+
if (jsonOutput) {
192+
ofstream output;
193+
output.open(outputfilename);
194+
output << testRun.GetAsJson();
195+
output.close();
196+
}
197+
165198
printf("Invalid bits per symbol.\n");
166199
print_usage();
167200
} else {
168201
data.word_size = inint;
169202
}
170203
}
171204

172-
if(verbose>1) {
173-
if(subsetSize == 0) printf("Opening file: '%s'\n", file_path);
205+
if (verbose > 1) {
206+
if (subsetSize == 0) printf("Opening file: '%s'\n", file_path);
174207
else printf("Opening file: '%s', reading block %ld of size %ld\n", file_path, subsetIndex, subsetSize);
175208
}
176209

@@ -318,7 +351,7 @@ int main(int argc, char* argv[]) {
318351
NonIidTestCase tc635;
319352

320353
if ((verbose == 1) || (verbose == 2)) printf("\nRunning Tuple Estimates...\n");
321-
354+
322355
if (((data.alph_size > 2) || !initial_entropy)) {
323356
SAalgs(data.bsymbols, data.blen, 2, bin_t_tuple_res, bin_lrs_res, verbose, "Bitstring");
324357
if (bin_t_tuple_res >= 0.0) {
@@ -340,7 +373,7 @@ int main(int argc, char* argv[]) {
340373
tc635.testCaseNumber = "T-Tuple Test";
341374
testRun.testCases.push_back(tc635);
342375

343-
// Section 6.3.6 - Estimate entropy with LRS Test
376+
// Section 6.3.6 - Estimate entropy with LRS Test
344377
NonIidTestCase tc636;
345378

346379
if ((((data.alph_size > 2) || !initial_entropy)) && (bin_lrs_res >= 0.0)) {
@@ -467,18 +500,18 @@ int main(int argc, char* argv[]) {
467500
h_assessed = min(h_assessed, H_original);
468501
}
469502

470-
if((verbose == 1) || (verbose == 2)) {
471-
if(initial_entropy) {
503+
if ((verbose == 1) || (verbose == 2)) {
504+
if (initial_entropy) {
472505
printf("\nH_original: %f\n", H_original);
473-
if(data.alph_size > 2) {
506+
if (data.alph_size > 2) {
474507
printf("H_bitstring: %f\n", H_bitstring);
475-
printf("min(H_original, %d X H_bitstring): %f\n", data.word_size, min(H_original, data.word_size*H_bitstring));
508+
printf("min(H_original, %d X H_bitstring): %f\n", data.word_size, min(H_original, data.word_size * H_bitstring));
476509
}
477510
} else {
478511
printf("\nh': %f\n", H_bitstring);
479512
}
480-
} else if(verbose > 2) {
481-
if((data.alph_size > 2) || !initial_entropy) {
513+
} else if (verbose > 2) {
514+
if ((data.alph_size > 2) || !initial_entropy) {
482515
printf("H_bitstring = %.17g\n", H_bitstring);
483516
printf("H_bitstring Per Symbol = %.17g\n", H_bitstring * data.word_size);
484517
}

0 commit comments

Comments
 (0)