00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <basis/function.h>
00019 #include <basis/guards.h>
00020 #include <basis/istring.h>
00021 #include <basis/string_array.h>
00022 #include <data_struct/static_memory_gremlin.h>
00023 #include <loggers/console_logger.h>
00024 #include <mechanisms/timer.h>
00025 #include <opsystem/application_shell.h>
00026 #include <opsystem/byte_filer.h>
00027 #include <opsystem/filename.h>
00028 #include <textual/list_parsing.h>
00029
00030
00031 const int MAX_DATA_FILE_ITERS = 4000;
00032
00033 #define LOG(s) EMERGENCY_LOG(program_wide_logger(), s)
00034
00035 class test_parsing_csv : public application_shell
00036 {
00037 public:
00038 test_parsing_csv();
00039 IMPLEMENT_CLASS_NAME("test_parsing_csv");
00040 int execute();
00041 };
00042
00043 test_parsing_csv::test_parsing_csv()
00044 : application_shell("t_parse_csv")
00045 {
00046 SET_DEFAULT_COMBO_LOGGER;
00047 }
00048
00049 #define COMPLAIN_FIELD(list, index, value) \
00050 log(isprintf("field %d: ", index) + list[index]); \
00051 if (list[index] != value) { \
00052 deadly_error(class_name(), "comparison test", isprintf("field %d is " \
00053 "erroneous in ", index) + #list); \
00054 }
00055
00056 int test_parsing_csv::execute()
00057 {
00058 istring line1 = "\"fupe\",\"snoorp\",\"lutem\",\"fipe\"";
00059 string_array fields1;
00060 bool works1 = list_parsing::parse_csv_line(line1, fields1);
00061 if (!works1)
00062 deadly_error(class_name(), "first test", "fails to parse");
00063 LOG(isprintf("fields len now %d", fields1.length()));
00064 if (fields1.length() != 4)
00065 deadly_error(class_name(), "first test", "wrong count of strings found");
00066 COMPLAIN_FIELD(fields1, 0, "fupe");
00067 COMPLAIN_FIELD(fields1, 1, "snoorp");
00068 COMPLAIN_FIELD(fields1, 2, "lutem");
00069 COMPLAIN_FIELD(fields1, 3, "fipe");
00070
00071 istring line2 = "fupe,\"snoorp\",lutem,\"fipe\"";
00072 string_array fields2;
00073 bool works2 = list_parsing::parse_csv_line(line2, fields2);
00074 if (!works2)
00075 deadly_error(class_name(), "second test", "fails to parse");
00076 if (fields2.length() != 4)
00077 deadly_error(class_name(), "second test", "wrong count of strings found");
00078 COMPLAIN_FIELD(fields2, 0, "fupe");
00079 COMPLAIN_FIELD(fields2, 1, "snoorp");
00080 COMPLAIN_FIELD(fields2, 2, "lutem");
00081 COMPLAIN_FIELD(fields2, 3, "fipe");
00082
00083 istring line3 = "\"lowenburger\",\"wazizzle\",morphel";
00084 string_array fields3;
00085 bool works3 = list_parsing::parse_csv_line(line3, fields3);
00086 if (!works3)
00087 deadly_error(class_name(), "third test", "fails to parse");
00088 if (fields3.length() != 3)
00089 deadly_error(class_name(), "third test", "wrong count of strings found");
00090 COMPLAIN_FIELD(fields3, 0, "lowenburger");
00091 COMPLAIN_FIELD(fields3, 1, "wazizzle");
00092 COMPLAIN_FIELD(fields3, 2, "morphel");
00093
00094 istring line4 = "\"lowenburger\",\"wazizzle\",morphel,";
00095 string_array fields4;
00096 bool works4 = list_parsing::parse_csv_line(line4, fields4);
00097 if (!works4)
00098 deadly_error(class_name(), "fourth test", "fails to parse");
00099 if (fields4.length() != 4)
00100 deadly_error(class_name(), "fourth test", "wrong count of strings found");
00101 COMPLAIN_FIELD(fields4, 0, "lowenburger");
00102 COMPLAIN_FIELD(fields4, 1, "wazizzle");
00103 COMPLAIN_FIELD(fields4, 2, "morphel");
00104 COMPLAIN_FIELD(fields4, 3, "");
00105
00106 istring line5 = "\"lowenburger\",,";
00107 string_array fields5;
00108 bool works5 = list_parsing::parse_csv_line(line5, fields5);
00109 if (!works5)
00110 deadly_error(class_name(), "fifth test", "fails to parse");
00111 if (fields5.length() != 3)
00112 deadly_error(class_name(), "fifth test", "wrong count of strings found");
00113 COMPLAIN_FIELD(fields5, 0, "lowenburger");
00114 COMPLAIN_FIELD(fields5, 1, "");
00115 COMPLAIN_FIELD(fields5, 2, "");
00116
00117 istring line6 = ",,,\"rasputy\",,\"spunk\",ralph";
00118 string_array fields6;
00119 bool works6 = list_parsing::parse_csv_line(line6, fields6);
00120 if (!works6)
00121 deadly_error(class_name(), "sixth test", "fails to parse");
00122 if (fields6.length() != 7)
00123 deadly_error(class_name(), "sixth test", "wrong count of strings found");
00124 COMPLAIN_FIELD(fields6, 0, "");
00125 COMPLAIN_FIELD(fields6, 1, "");
00126 COMPLAIN_FIELD(fields6, 2, "");
00127 COMPLAIN_FIELD(fields6, 3, "rasputy");
00128 COMPLAIN_FIELD(fields6, 4, "");
00129 COMPLAIN_FIELD(fields6, 5, "spunk");
00130 COMPLAIN_FIELD(fields6, 6, "ralph");
00131
00132 istring line7 = "\"SRV0001337CHN0000001DSP0000001SRV0001337LAY0003108,16,0,8,192\",\"\\\"row_3\\\" on 12.5.55.159\",3";
00133 string_array fields7;
00134 bool works7 = list_parsing::parse_csv_line(line7, fields7);
00135 if (!works7)
00136 deadly_error(class_name(), "seventh test", "fails to parse");
00137 if (fields7.length() != 3)
00138 deadly_error(class_name(), "seventh test", "wrong count of strings found");
00139 COMPLAIN_FIELD(fields7, 0, "SRV0001337CHN0000001DSP0000001SRV0001337LAY0003108,16,0,8,192");
00140 COMPLAIN_FIELD(fields7, 1, "\"row_3\" on 12.5.55.159");
00141 COMPLAIN_FIELD(fields7, 2, "3");
00142
00143
00144 filename df_dir = filename(portable::application_name()).dirname();
00145 byte_filer test_data(df_dir.raw() + "/df_1.csv", "rb");
00146 string_array parsed;
00147 string_array lines;
00148 istring curr_line;
00149 int read_result;
00150 while ( (read_result = test_data.getline(curr_line, 1024)) > 0 )
00151 lines += curr_line;
00152 if (lines.length()) {
00153
00154 timer stopwatch;
00155 stopwatch.start();
00156 for (int iterations = 0; iterations < MAX_DATA_FILE_ITERS; iterations++) {
00157 for (int line = 0; line < lines.length(); line++) {
00158 const istring ¤t = lines[line];
00159 list_parsing::parse_csv_line(current, parsed);
00160 }
00161 }
00162 stopwatch.stop();
00163 log(isprintf("%d csv lines with %d iterations took %d ms (or %d s).",
00164 lines.length(), MAX_DATA_FILE_ITERS, stopwatch.elapsed(),
00165 stopwatch.elapsed() / 1000));
00166 }
00167
00168
00169 string_array fields9;
00170 fields9 += "ACk\"boozort";
00171 fields9 += "sme\"ra\"\"foop";
00172 fields9 += "\"gumby\"";
00173 istring line9 = "\"ACk\\\"boozort\",\"sme\\\"ra\\\"\\\"foop\",\"\\\"gumby\\\"\"";
00174 istring gen_line_9;
00175 list_parsing::create_csv_line(fields9, gen_line_9);
00176
00177
00178 if (gen_line_9 != line9)
00179 deadly_error(class_name(), "ninth test", "failed to create expected text");
00180 string_array fields9_b;
00181 bool works9 = list_parsing::parse_csv_line(gen_line_9, fields9_b);
00182 if (!works9)
00183 deadly_error(class_name(), "ninth test", "fails to parse");
00184 if (fields9_b != fields9)
00185 deadly_error(class_name(), "ninth test", "failed to match original fields");
00186
00187 guards::alert_message("list_parsing::parse_csv_line: "
00188 "works for common examples.");
00189
00190 return 0;
00191 }
00192
00194
00195 HOOPLE_MAIN(test_parsing_csv, )
00196