diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-10-17 20:15:23 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-10-23 18:17:02 -0700 |
commit | d508a16aca91b521719d929cd59db381c44cd7ad (patch) | |
tree | af076eb66fcd4002a01a29a46f8b80d4d4129994 /tests | |
parent | b9b51ffd4ebd14e085bef69ee0daf6a1927cc960 (diff) | |
download | subsurface-d508a16aca91b521719d929cd59db381c44cd7ad.tar.gz |
parser: replace params[] code by new xml_params struct
This fixes a load of memory holes, and makes the code
(hopefully) more readable.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/testparse.cpp | 341 |
1 files changed, 116 insertions, 225 deletions
diff --git a/tests/testparse.cpp b/tests/testparse.cpp index f5b93b996..58bb6a153 100644 --- a/tests/testparse.cpp +++ b/tests/testparse.cpp @@ -8,6 +8,7 @@ #include "core/parse.h" #include "core/qthelper.h" #include "core/subsurface-string.h" +#include "core/xmlparams.h" #include <QTextStream> /* We have to use a macro since QCOMPARE @@ -53,62 +54,35 @@ int TestParse::parseCSV(int units, std::string file) // // CSV import should work verbose = 1; - char *params[55]; - int pnr = 0; - - params[pnr++] = strdup("numberField"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("dateField"); - params[pnr++] = intdup(1); - params[pnr++] = strdup("timeField"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("durationField"); - params[pnr++] = intdup(3); - params[pnr++] = strdup("locationField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("gpsField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("maxDepthField"); - params[pnr++] = intdup(4); - params[pnr++] = strdup("meanDepthField"); - params[pnr++] = intdup(5); - params[pnr++] = strdup("divemasterField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("buddyField"); - params[pnr++] = intdup(6); - params[pnr++] = strdup("suitField"); - params[pnr++] = intdup(7); - params[pnr++] = strdup("notesField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("weightField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("tagsField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("separatorIndex"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("units"); - params[pnr++] = intdup(units); - params[pnr++] = strdup("datefmt"); - params[pnr++] = intdup(1); - params[pnr++] = strdup("durationfmt"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("cylindersizeField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("startpressureField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("endpressureField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("o2Field"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("heField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("airtempField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("watertempField"); - params[pnr++] = intdup(-1); - params[pnr++] = NULL; - - return parse_manual_file(file.c_str(), params, pnr - 1, &dive_table, &trip_table, &dive_site_table, &filter_preset_table); + xml_params params; + + xml_params_add_int(¶ms, "numberField", 0); + xml_params_add_int(¶ms, "dateField", 1); + xml_params_add_int(¶ms, "timeField", 2); + xml_params_add_int(¶ms, "durationField", 3); + xml_params_add_int(¶ms, "locationField", -1); + xml_params_add_int(¶ms, "gpsField", -1); + xml_params_add_int(¶ms, "maxDepthField", 4); + xml_params_add_int(¶ms, "meanDepthField", 5); + xml_params_add_int(¶ms, "divemasterField", -1); + xml_params_add_int(¶ms, "buddyField", 6); + xml_params_add_int(¶ms, "suitField", 7); + xml_params_add_int(¶ms, "notesField", -1); + xml_params_add_int(¶ms, "weightField", -1); + xml_params_add_int(¶ms, "tagsField", -1); + xml_params_add_int(¶ms, "separatorIndex", 0); + xml_params_add_int(¶ms, "units", units); + xml_params_add_int(¶ms, "datefmt", 1); + xml_params_add_int(¶ms, "durationfmt", 2); + xml_params_add_int(¶ms, "cylindersizeField", -1); + xml_params_add_int(¶ms, "startpressureField", -1); + xml_params_add_int(¶ms, "endpressureField", -1); + xml_params_add_int(¶ms, "o2Field", -1); + xml_params_add_int(¶ms, "heField", -1); + xml_params_add_int(¶ms, "airtempField", -1); + xml_params_add_int(¶ms, "watertempField", -1); + + return parse_manual_file(file.c_str(), ¶ms, &dive_table, &trip_table, &dive_site_table, &filter_preset_table); } int TestParse::parseDivingLog() @@ -179,45 +153,27 @@ void TestParse::testParseDM5() void TestParse::testParseHUDC() { - char *params[37]; - int pnr = 0; - - params[pnr++] = strdup("timeField"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("depthField"); - params[pnr++] = intdup(1); - params[pnr++] = strdup("tempField"); - params[pnr++] = intdup(5); - params[pnr++] = strdup("po2Field"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("o2sensor1Field"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("o2sensor2Field"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("o2sensor3Field"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("cnsField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("ndlField"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("ttsField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("stopdepthField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("pressureField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("setpointField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("separatorIndex"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("units"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("hw"); - params[pnr++] = strdup("\"DC text\""); - params[pnr++] = NULL; + xml_params params; + + xml_params_add_int(¶ms, "timeField", 0); + xml_params_add_int(¶ms, "depthField", 1); + xml_params_add_int(¶ms, "tempField", 5); + xml_params_add_int(¶ms, "po2Field", -1); + xml_params_add_int(¶ms, "o2sensor1Field", -1); + xml_params_add_int(¶ms, "o2sensor2Field", -1); + xml_params_add_int(¶ms, "o2sensor3Field", -1); + xml_params_add_int(¶ms, "cnsField", -1); + xml_params_add_int(¶ms, "ndlField", 2); + xml_params_add_int(¶ms, "ttsField", -1); + xml_params_add_int(¶ms, "stopdepthField", -1); + xml_params_add_int(¶ms, "pressureField", -1); + xml_params_add_int(¶ms, "setpointField", -1); + xml_params_add_int(¶ms, "separatorIndex", 2); + xml_params_add_int(¶ms, "units", 0); + xml_params_add(¶ms, "hw", "\"DC text\""); QCOMPARE(parse_csv_file(SUBSURFACE_TEST_DATA "/dives/TestDiveSeabearHUDC.csv", - params, pnr - 1, "csv", &dive_table, &trip_table, &dive_site_table, &filter_preset_table), + ¶ms, "csv", &dive_table, &trip_table, &dive_site_table, &filter_preset_table), 0); QCOMPARE(dive_table.nr, 1); @@ -306,67 +262,39 @@ void TestParse::testParseMerge() int TestParse::parseCSVmanual(int units, std::string file) { verbose = 1; - char *params[59]; - int pnr = 0; + xml_params params; // Numbers are column numbers - params[pnr++] = strdup("numberField"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("dateField"); - params[pnr++] = intdup(1); - params[pnr++] = strdup("timeField"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("durationField"); - params[pnr++] = intdup(3); + xml_params_add_int(¶ms, "numberField", 0); + xml_params_add_int(¶ms, "dateField", 1); + xml_params_add_int(¶ms, "timeField", 2); + xml_params_add_int(¶ms, "durationField", 3); // 4 Will be SAC, once we add support for reading it - params[pnr++] = strdup("maxDepthField"); - params[pnr++] = intdup(5); - params[pnr++] = strdup("meanDepthField"); - params[pnr++] = intdup(6); - params[pnr++] = strdup("modeField"); - params[pnr++] = intdup(7); - params[pnr++] = strdup("airtempField"); - params[pnr++] = intdup(8); - params[pnr++] = strdup("watertempField"); - params[pnr++] = intdup(9); - params[pnr++] = strdup("cylindersizeField"); - params[pnr++] = intdup(10); - params[pnr++] = strdup("startpressureField"); - params[pnr++] = intdup(11); - params[pnr++] = strdup("endpressureField"); - params[pnr++] = intdup(12); - params[pnr++] = strdup("o2Field"); - params[pnr++] = intdup(13); - params[pnr++] = strdup("heField"); - params[pnr++] = intdup(14); - params[pnr++] = strdup("locationField"); - params[pnr++] = intdup(15); - params[pnr++] = strdup("gpsField"); - params[pnr++] = intdup(16); - params[pnr++] = strdup("divemasterField"); - params[pnr++] = intdup(17); - params[pnr++] = strdup("buddyField"); - params[pnr++] = intdup(18); - params[pnr++] = strdup("suitField"); - params[pnr++] = intdup(19); - params[pnr++] = strdup("notesField"); - params[pnr++] = intdup(22); - params[pnr++] = strdup("weightField"); - params[pnr++] = intdup(23); - params[pnr++] = strdup("tagsField"); - params[pnr++] = intdup(24); + xml_params_add_int(¶ms, "maxDepthField", 5); + xml_params_add_int(¶ms, "meanDepthField", 6); + xml_params_add_int(¶ms, "modeField", 7); + xml_params_add_int(¶ms, "airtempField", 8); + xml_params_add_int(¶ms, "watertempField", 9); + xml_params_add_int(¶ms, "cylindersizeField", 10); + xml_params_add_int(¶ms, "startpressureField", 11); + xml_params_add_int(¶ms, "endpressureField", 12); + xml_params_add_int(¶ms, "o2Field", 13); + xml_params_add_int(¶ms, "heField", 14); + xml_params_add_int(¶ms, "locationField", 15); + xml_params_add_int(¶ms, "gpsField", 16); + xml_params_add_int(¶ms, "divemasterField", 17); + xml_params_add_int(¶ms, "buddyField", 18); + xml_params_add_int(¶ms, "suitField", 19); + xml_params_add_int(¶ms, "notesField", 22); + xml_params_add_int(¶ms, "weightField", 23); + xml_params_add_int(¶ms, "tagsField", 24); // Numbers are indices of possible options - params[pnr++] = strdup("separatorIndex"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("datefmt"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("durationfmt"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("units"); - params[pnr++] = intdup(units); - params[pnr++] = NULL; - - return parse_manual_file(file.c_str(), params, pnr - 1, &dive_table, &trip_table, &dive_site_table, &filter_preset_table); + xml_params_add_int(¶ms, "separatorIndex", 0); + xml_params_add_int(¶ms, "datefmt", 2); + xml_params_add_int(¶ms, "durationfmt", 2); + xml_params_add_int(¶ms, "units", units); + + return parse_manual_file(file.c_str(), ¶ms, &dive_table, &trip_table, &dive_site_table, &filter_preset_table); } void TestParse::exportCSVDiveDetails() @@ -405,8 +333,7 @@ void TestParse::exportCSVDiveDetails() void TestParse::exportSubsurfaceCSV() { int saved_sac = 0; - char *params[59]; - int pnr = 0; + xml_params params; /* Test SubsurfaceCSV with multiple cylinders */ parse_file(SUBSURFACE_TEST_DATA "/dives/test40.xml", &dive_table, &trip_table, &dive_site_table, &filter_preset_table); @@ -421,12 +348,9 @@ void TestParse::exportSubsurfaceCSV() clear_dive_file_data(); - params[pnr++] = strdup("separatorIndex"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("units"); - params[pnr++] = intdup(1); - params[pnr++] = 0; - parse_csv_file("testcsvexportmanualimperial-cyl.csv", params, pnr - 1, "SubsurfaceCSV", &dive_table, &trip_table, &dive_site_table, &filter_preset_table); + xml_params_add_int(¶ms, "separatorIndex", 0); + xml_params_add_int(¶ms, "units", 1); + parse_csv_file("testcsvexportmanualimperial-cyl.csv", ¶ms, "SubsurfaceCSV", &dive_table, &trip_table, &dive_site_table, &filter_preset_table); // We do not currently support reading SAC, thus faking it if (dive_table.nr > 0) { @@ -445,32 +369,21 @@ void TestParse::exportSubsurfaceCSV() int TestParse::parseCSVprofile(int units, std::string file) { verbose = 1; - char *params[55]; - int pnr = 0; + xml_params params; // Numbers are column numbers - params[pnr++] = strdup("numberField"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("dateField"); - params[pnr++] = intdup(1); - params[pnr++] = strdup("starttimeField"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("timeField"); - params[pnr++] = intdup(3); - params[pnr++] = strdup("depthField"); - params[pnr++] = intdup(4); - params[pnr++] = strdup("tempField"); - params[pnr++] = intdup(5); - params[pnr++] = strdup("pressureField"); - params[pnr++] = intdup(6); + xml_params_add_int(¶ms, "numberField", 0); + xml_params_add_int(¶ms, "dateField", 1); + xml_params_add_int(¶ms, "starttimeField", 2); + xml_params_add_int(¶ms, "timeField", 3); + xml_params_add_int(¶ms, "depthField", 4); + xml_params_add_int(¶ms, "tempField", 5); + xml_params_add_int(¶ms, "pressureField", 6); // Numbers are indices of possible options - params[pnr++] = strdup("datefmt"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("units"); - params[pnr++] = intdup(units); - params[pnr++] = NULL; + xml_params_add_int(¶ms, "datefmt", 2); + xml_params_add_int(¶ms, "units", units); - return parse_csv_file(file.c_str(), params, pnr - 1, "csv", &dive_table, &trip_table, &dive_site_table, &filter_preset_table); + return parse_csv_file(file.c_str(), ¶ms, "csv", &dive_table, &trip_table, &dive_site_table, &filter_preset_table); } void TestParse::exportCSVDiveProfile() @@ -518,54 +431,32 @@ void TestParse::testExport() void TestParse::parseDL7() { - char *params[51]; - int pnr = 0; - - params[pnr++] = strdup("dateField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("datefmt"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("starttimeField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("numberField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("timeField"); - params[pnr++] = intdup(1); - params[pnr++] = strdup("depthField"); - params[pnr++] = intdup(2); - params[pnr++] = strdup("tempField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("po2Field"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("o2sensor1Field"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("o2sensor2Field"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("o2sensor3Field"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("cnsField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("ndlField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("ttsField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("stopdepthField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("pressureField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("setpointField"); - params[pnr++] = intdup(-1); - params[pnr++] = strdup("separatorIndex"); - params[pnr++] = intdup(3); - params[pnr++] = strdup("units"); - params[pnr++] = intdup(0); - params[pnr++] = strdup("hw"); - params[pnr++] = strdup("DL7"); - params[pnr++] = 0; + xml_params params; + + xml_params_add_int(¶ms, "dateField", -1); + xml_params_add_int(¶ms, "datefmt", 0); + xml_params_add_int(¶ms, "starttimeField", -1); + xml_params_add_int(¶ms, "numberField", -1); + xml_params_add_int(¶ms, "timeField", 1); + xml_params_add_int(¶ms, "depthField", 2); + xml_params_add_int(¶ms, "tempField", -1); + xml_params_add_int(¶ms, "po2Field", -1); + xml_params_add_int(¶ms, "o2sensor1Field", -1); + xml_params_add_int(¶ms, "o2sensor2Field", -1); + xml_params_add_int(¶ms, "o2sensor3Field", -1); + xml_params_add_int(¶ms, "cnsField", -1); + xml_params_add_int(¶ms, "ndlField", -1); + xml_params_add_int(¶ms, "ttsField", -1); + xml_params_add_int(¶ms, "stopdepthField", -1); + xml_params_add_int(¶ms, "pressureField", -1); + xml_params_add_int(¶ms, "setpointField", -1); + xml_params_add_int(¶ms, "separatorIndex", 3); + xml_params_add_int(¶ms, "units", 0); + xml_params_add(¶ms, "hw", "DL7"); clear_dive_file_data(); QCOMPARE(parse_csv_file(SUBSURFACE_TEST_DATA "/dives/DL7.zxu", - params, pnr - 1, "DL7", &dive_table, &trip_table, &dive_site_table, &filter_preset_table), + ¶ms, "DL7", &dive_table, &trip_table, &dive_site_table, &filter_preset_table), 0); QCOMPARE(dive_table.nr, 3); |