parse_value_string_test.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // test parse_value_string()
4 //
5 // ======================================================================
6 
7 #define BOOST_TEST_MODULE (document test)
8 #include "boost/test/unit_test.hpp"
9 
11 #include "fhiclcpp/parse.h"
12 
13 #include <string>
14 #include <utility>
15 
18 using std::pair;
19 using std::string;
20 
21 namespace {
22  string const nil(9, '\0');
23  string const failed(8, '\0');
24 
26  parse_as(string const& input)
27  {
29  string unparsed;
30  return parse_value_string(input, result, unparsed) ? result.to_string() :
31  failed;
32  }
33 
34  string
35  dquoted(string s)
36  {
37  return '"' + s + '"';
38  }
39 
40  string
41  squoted(string s)
42  {
43  return '\'' + s + '\'';
44  }
45 }
46 
47 BOOST_AUTO_TEST_SUITE(document_test)
48 
50 {
51  BOOST_CHECK(parse_as("@nil") == nil);
52  BOOST_CHECK(parse_as(" @nil") == nil);
53  BOOST_CHECK(parse_as(" @nil ") == nil);
54  BOOST_CHECK(parse_as(" true or false ") == failed);
55  BOOST_CHECK(parse_as(" TRUE or FALSE ") == failed);
56  BOOST_CHECK(parse_as("infinity") == "+infinity");
57  BOOST_CHECK(parse_as(" -infinity") == "-infinity");
58  BOOST_CHECK(parse_as("+ infinity") == failed);
59  BOOST_CHECK(parse_as("nil_") == dquoted("nil_"));
60 }
61 
63 {
64  BOOST_CHECK(parse_as("0") == "0");
65  BOOST_CHECK(parse_as("000") == "0");
66  BOOST_CHECK(parse_as("123456") == "123456");
67  BOOST_CHECK(parse_as("1234567") == "1.234567e6");
68  BOOST_CHECK(parse_as("0.0") == "0");
69  BOOST_CHECK(parse_as("0.") == "0");
70 
71  BOOST_CHECK(parse_as("+0") == "0");
72  BOOST_CHECK(parse_as("+000") == "0");
73  BOOST_CHECK(parse_as("+123456") == "123456");
74  BOOST_CHECK(parse_as("+1234567") == "1.234567e6");
75  BOOST_CHECK(parse_as("-0") == "0");
76  BOOST_CHECK(parse_as("-000") == "0");
77  BOOST_CHECK(parse_as("-123456") == "-123456");
78  BOOST_CHECK(parse_as("-1234567") == "-1.234567e6");
79  BOOST_CHECK(parse_as("1.23e-4") == "1.23e-4");
80 
81  BOOST_CHECK(parse_as("+0.0E0") == "0");
82  BOOST_CHECK(parse_as("+00.") == "0");
83  BOOST_CHECK(parse_as("-0e99") == "0");
84  BOOST_CHECK(parse_as("-.00") == "0");
85  BOOST_CHECK(parse_as("+123456e0") == "123456");
86  BOOST_CHECK(parse_as("+1234567e-0") == "1.234567e6");
87 }
88 
89 BOOST_AUTO_TEST_CASE(everything_else)
90 {
91  BOOST_CHECK(parse_as("Hello") == dquoted("Hello"));
92  BOOST_CHECK(parse_as("_123") == dquoted("_123"));
93  BOOST_CHECK(parse_as("_123Aa_") == dquoted("_123Aa_"));
94  BOOST_CHECK(parse_as("X...") == failed);
95  BOOST_CHECK(parse_as("3a4B5c") == dquoted("3a4B5c"));
96  BOOST_CHECK(parse_as("0123Aa_") == dquoted("0123Aa_"));
97  BOOST_CHECK(parse_as("9...") == failed);
98  BOOST_CHECK(parse_as("nil") == dquoted("nil"));
99  BOOST_CHECK(parse_as(R"($\d+^)") == failed);
100 
101  BOOST_CHECK(parse_as(" ( 1 , 2.3 ) ") == "(1,2.3)");
102  BOOST_CHECK(parse_as(" ( infinity , -infinity ) ") ==
103  "(+infinity,-infinity)");
104  BOOST_CHECK(parse_as(dquoted("Hi, there!")) == dquoted("Hi, there!"));
105  BOOST_CHECK(parse_as(dquoted("\\t\\n")) == dquoted("\\t\\n"));
106  BOOST_CHECK(parse_as(squoted("Hi, there!")) == dquoted("Hi, there!"));
107  BOOST_CHECK(parse_as(squoted("\\t\\n")) == dquoted("\\\\t\\\\n"));
108  BOOST_CHECK(parse_as("infinityx") == dquoted("infinityx"));
109  BOOST_CHECK(parse_as(dquoted("Hi, \\\"Tom\\\"")) ==
110  dquoted("Hi, \\\"Tom\\\""));
111  BOOST_CHECK(parse_as(dquoted("@Nil")) == dquoted("@Nil"));
112 
113  BOOST_CHECK(parse_as(" [1,2.3] ") == "[1,2.3]");
114  BOOST_CHECK(parse_as(" [ 1 , 2.3 ] ") == "[1,2.3]");
115  BOOST_CHECK(parse_as(" [hello,there] ") ==
116  "[" + dquoted("hello") + "," + dquoted("there") + "]");
117  BOOST_CHECK(parse_as("[ hello,\nthere\t] ") ==
118  "[" + dquoted("hello") + "," + dquoted("there") + "]");
119  BOOST_CHECK(parse_as(" [ ] ") == "[]");
120 
121  BOOST_CHECK(parse_as("{ }") == "{}");
122  BOOST_CHECK(parse_as("{a : 1.2 }") == "{a:1.2}");
123  BOOST_CHECK(parse_as("{a : 1.2 b: hello}") ==
124  "{a:1.2 b:" + dquoted("hello") + "}");
125  BOOST_CHECK(parse_as("{a : 3 b: 7 a: @erase}") == "{b:7}");
126 }
127 
128 BOOST_AUTO_TEST_SUITE_END()
bool parse_value_string(std::string const &s, extended_value &v, std::string &unparsed)
Definition: parse.cc:669
static QCString result
std::string dquoted(std::string const &s)
std::string string
Definition: nybbler.cc:12
static int input(void)
Definition: code.cpp:15695
BOOST_AUTO_TEST_CASE(symbols)
std::string squoted(std::string const &s)
std::string to_string() const
static QCString * s
Definition: config.cpp:1042