From f020169c261bf6ed44ebb9a73d1d4de295aa68e4 Mon Sep 17 00:00:00 2001 From: Simon Otter Date: Mon, 23 Feb 2015 23:34:16 +0100 Subject: [PATCH] Created a bunch of tests for the parser, and a spec. --- float_parser_tests.cc | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 float_parser_tests.cc diff --git a/float_parser_tests.cc b/float_parser_tests.cc new file mode 100644 index 0000000..e8526ec --- /dev/null +++ b/float_parser_tests.cc @@ -0,0 +1,55 @@ +#include +#include + +// Tries to parse a floating point number located at s. +// +// Parses the following EBNF grammar: +// sign = "+" | "-" ; +// END = ? anything not in digit ? +// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; +// integer = [sign] , digit , {digit} ; +// decimal = integer , ["." , {digit}] ; +// float = ( decimal , END ) | ( decimal , ("E" | "e") , decimal , END ) ; +// +// Valid strings are for example: +// -0 +3.1417e+2 -0.E-3 1.0324 -1.41 11e2 +// +// If the parsing is a success, result is set to the parsed value and true +// is returned. +// +// The function is greedy, it will parse until it encounters a null-byte or +// a non-conforming character is encountered. +// +// The following situations triggers a failure: +// - parse failure. +// - underflow/overflow. +// +bool tryParseFloat(const char *s, double *result) +{ + return false; +} + +void testParsing(const char *input, bool expectedRes, double expectedValue) +{ + double val = 0.0; + bool res = tryParseFloat(input, &val); + if (res != expectedRes || val != expectedValue) + { + printf("% 20s failed, returned %d and value % 10.4f.\n", input, res, val); + } +} + +int main(int argc, char const *argv[]) +{ + testParsing("0", true, 0.0); + testParsing("-0", true, 0.0); + testParsing("+0", true, 0.0); + testParsing("1", true, 1.0); + testParsing("+1", true, 1.0); + testParsing("-1", true, -1.0); + testParsing("-1.08", true, -1.08); + testParsing("100.0823blabla", true, 100.0823); + testParsing("34.E-2\04", true, 34.0e-2); + testParsing("+34.23E2\02", true, 34.23E2); + return 0; +} \ No newline at end of file