| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2015 Joel de Guzman |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | =============================================================================*/ |
| 7 | #include <boost/spirit/home/x3.hpp> |
| 8 | |
| 9 | #include <iostream> |
| 10 | #include "test.hpp" |
| 11 | |
| 12 | // Custom string type with a C-style string conversion. |
| 13 | struct custom_string_c |
| 14 | { |
| 15 | custom_string_c(char c) { str[0] = c; str[1] = '\0'; } |
| 16 | |
| 17 | operator char*() { return str; } |
| 18 | operator char const*() const { return str; } |
| 19 | |
| 20 | private: |
| 21 | char str[2]; |
| 22 | }; |
| 23 | |
| 24 | std::string get_str(char const* str) |
| 25 | { |
| 26 | return std::string(str); |
| 27 | } |
| 28 | |
| 29 | int |
| 30 | main() |
| 31 | { |
| 32 | using spirit_test::test; |
| 33 | using spirit_test::test_attr; |
| 34 | using boost::spirit::x3::symbols; |
| 35 | using boost::spirit::x3::rule; |
| 36 | |
| 37 | { // construction from symbol array |
| 38 | char const* syms[] = {"Joel" ,"Ruby" ,"Tenji" ,"Tutit" ,"Kim" ,"Joey" }; |
| 39 | symbols<int> sym(syms); |
| 40 | |
| 41 | BOOST_TEST((test("Joel" , sym))); |
| 42 | BOOST_TEST((test("Ruby" , sym))); |
| 43 | BOOST_TEST((test("Tenji" , sym))); |
| 44 | BOOST_TEST((test("Tutit" , sym))); |
| 45 | BOOST_TEST((test("Kim" , sym))); |
| 46 | BOOST_TEST((test("Joey" , sym))); |
| 47 | BOOST_TEST((!test("XXX" , sym))); |
| 48 | } |
| 49 | |
| 50 | { // construction from 2 arrays |
| 51 | |
| 52 | char const* syms[] = {"Joel" ,"Ruby" ,"Tenji" ,"Tutit" ,"Kim" ,"Joey" }; |
| 53 | int data[] = {1,2,3,4,5,6}; |
| 54 | symbols<int> sym(syms, data); |
| 55 | |
| 56 | int i; |
| 57 | BOOST_TEST((test_attr("Joel" , sym, i))); |
| 58 | BOOST_TEST(i == 1); |
| 59 | BOOST_TEST((test_attr("Ruby" , sym, i))); |
| 60 | BOOST_TEST(i == 2); |
| 61 | BOOST_TEST((test_attr("Tenji" , sym, i))); |
| 62 | BOOST_TEST(i == 3); |
| 63 | BOOST_TEST((test_attr("Tutit" , sym, i))); |
| 64 | BOOST_TEST(i == 4); |
| 65 | BOOST_TEST((test_attr("Kim" , sym, i))); |
| 66 | BOOST_TEST(i == 5); |
| 67 | BOOST_TEST((test_attr("Joey" , sym, i))); |
| 68 | BOOST_TEST(i == 6); |
| 69 | BOOST_TEST((!test_attr("XXX" , sym, i))); |
| 70 | } |
| 71 | |
| 72 | { // allow std::string and other string types |
| 73 | symbols<> sym; |
| 74 | |
| 75 | // const and non-const std::string |
| 76 | std::string a("abc" ); |
| 77 | std::string const b("def" ); |
| 78 | sym += a; |
| 79 | sym += b; |
| 80 | BOOST_TEST((test("abc" , sym))); |
| 81 | BOOST_TEST((test("def" , sym))); |
| 82 | sym = a; |
| 83 | BOOST_TEST((test("abc" , sym))); |
| 84 | BOOST_TEST((!test("def" , sym))); |
| 85 | |
| 86 | // non-const C-style string |
| 87 | char arr[2]; arr[0] = 'a'; arr[1] = '\0'; |
| 88 | sym = arr; |
| 89 | BOOST_TEST((test("a" , sym))); |
| 90 | BOOST_TEST((!test("b" , sym))); |
| 91 | |
| 92 | // const and non-const custom string type |
| 93 | custom_string_c c('x'); |
| 94 | custom_string_c const cc('y'); |
| 95 | sym = c, cc; |
| 96 | BOOST_TEST((test("x" , sym))); |
| 97 | BOOST_TEST((test("y" , sym))); |
| 98 | BOOST_TEST((!test("z" , sym))); |
| 99 | } |
| 100 | |
| 101 | { // find |
| 102 | |
| 103 | symbols<int> sym; |
| 104 | sym.add("a" , 1)("b" , 2); |
| 105 | |
| 106 | BOOST_TEST(!sym.find("c" )); |
| 107 | |
| 108 | BOOST_TEST(sym.find("a" ) && *sym.find("a" ) == 1); |
| 109 | BOOST_TEST(sym.find("b" ) && *sym.find("b" ) == 2); |
| 110 | |
| 111 | BOOST_TEST(sym.at("a" ) == 1); |
| 112 | BOOST_TEST(sym.at("b" ) == 2); |
| 113 | BOOST_TEST(sym.at("c" ) == 0); |
| 114 | |
| 115 | BOOST_TEST(sym.find("a" ) && *sym.find("a" ) == 1); |
| 116 | BOOST_TEST(sym.find("b" ) && *sym.find("b" ) == 2); |
| 117 | BOOST_TEST(sym.find("c" ) && *sym.find("c" ) == 0); |
| 118 | |
| 119 | symbols<int> const_sym(sym); |
| 120 | |
| 121 | BOOST_TEST(const_sym.find("a" ) && *const_sym.find("a" ) == 1); |
| 122 | BOOST_TEST(const_sym.find("b" ) && *const_sym.find("b" ) == 2); |
| 123 | BOOST_TEST(const_sym.find("c" ) && *const_sym.find("c" ) == 0); |
| 124 | BOOST_TEST(!const_sym.find("d" )); |
| 125 | |
| 126 | char const *str1 = "all" ; |
| 127 | char const *first = str1, *last = str1 + 3; |
| 128 | BOOST_TEST(*sym.prefix_find(first, last) == 1 && first == str1 + 1); |
| 129 | |
| 130 | char const *str2 = "dart" ; |
| 131 | first = str2; last = str2 + 4; |
| 132 | BOOST_TEST(!sym.prefix_find(first, last) && first == str2); |
| 133 | } |
| 134 | |
| 135 | { // name |
| 136 | symbols <int> sym,sym2; |
| 137 | sym.name(str: "test" ); |
| 138 | BOOST_TEST(sym.name()=="test" ); |
| 139 | sym2 = sym; |
| 140 | BOOST_TEST(sym2.name()=="test" ); |
| 141 | |
| 142 | symbols <int> sym3(sym); |
| 143 | BOOST_TEST(sym3.name()=="test" ); |
| 144 | } |
| 145 | |
| 146 | { // Substrings |
| 147 | |
| 148 | symbols<int> sym; |
| 149 | BOOST_TEST(sym.at("foo" ) == 0); |
| 150 | sym.at(str: "foo" ) = 1; |
| 151 | BOOST_TEST(sym.at("foo" ) == 1); |
| 152 | BOOST_TEST(sym.at("fool" ) == 0); |
| 153 | sym.at(str: "fool" ) = 2; |
| 154 | BOOST_TEST(sym.find("foo" ) && *sym.find("foo" ) == 1); |
| 155 | BOOST_TEST(sym.find("fool" ) && *sym.find("fool" ) == 2); |
| 156 | BOOST_TEST(!sym.find("foolish" )); |
| 157 | BOOST_TEST(!sym.find("foot" )); |
| 158 | BOOST_TEST(!sym.find("afoot" )); |
| 159 | |
| 160 | char const *str, *first, *last; |
| 161 | str = "foolish" ; first = str; last = str + 7; |
| 162 | BOOST_TEST(*sym.prefix_find(first, last) == 2 && first == str + 4); |
| 163 | |
| 164 | first = str; last = str + 4; |
| 165 | BOOST_TEST(*sym.prefix_find(first, last) == 2 && first == str + 4); |
| 166 | |
| 167 | str = "food" ; first = str; last = str + 4; |
| 168 | BOOST_TEST(*sym.prefix_find(first, last) == 1 && first == str + 3); |
| 169 | |
| 170 | first = str; last = str + 3; |
| 171 | BOOST_TEST(*sym.prefix_find(first, last) == 1 && first == str + 3); |
| 172 | |
| 173 | first = str; last = str + 2; |
| 174 | BOOST_TEST(!sym.prefix_find(first, last) && first == str); |
| 175 | } |
| 176 | |
| 177 | { |
| 178 | // remove bug |
| 179 | |
| 180 | std::string s; |
| 181 | symbols<double> vars; |
| 182 | |
| 183 | vars.add("l1" , 12.0); |
| 184 | vars.add("l2" , 0.0); |
| 185 | vars.remove("l2" ); |
| 186 | vars.find(str: "l1" ); |
| 187 | double* d = vars.find(str: "l1" ); |
| 188 | BOOST_TEST(d != 0); |
| 189 | } |
| 190 | |
| 191 | { // test for proto problem with rvalue references (10-11-2011) |
| 192 | symbols<int> sym; |
| 193 | sym += get_str(str: "Joel" ); |
| 194 | sym += get_str(str: "Ruby" ); |
| 195 | |
| 196 | BOOST_TEST((test("Joel" , sym))); |
| 197 | BOOST_TEST((test("Ruby" , sym))); |
| 198 | } |
| 199 | |
| 200 | return boost::report_errors(); |
| 201 | } |
| 202 | |