Goby v2
as.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include "goby/util/as.h"
23 
24 namespace test
25 {
26 bool isnan(double a) { return a != a; }
27 } // namespace test
28 
29 using goby::util::as;
30 
31 enum MyEnum
32 {
33  BAZ = 0,
34  FOO = 1,
35  BAR = 2
36 };
37 
38 class MyClass
39 {
40  public:
41  MyClass(int a = 0, std::string b = "") : a(a), b(b) {}
42 
43  bool operator==(const MyClass& other) { return (other.a == a) && (other.b == b); }
44 
45  friend std::istream& operator>>(std::istream& is, MyClass& obj);
46  friend std::ostream& operator<<(std::ostream& os, const MyClass& obj);
47 
48  private:
49  int a;
50  std::string b;
51 };
52 
53 std::istream& operator>>(std::istream& is, MyClass& obj)
54 {
55  is >> obj.a;
56  is.ignore(1);
57  is >> obj.b;
58  return is;
59 }
60 std::ostream& operator<<(std::ostream& os, const MyClass& obj)
61 {
62  os << obj.a << "!" << obj.b;
63  return os;
64 }
65 
66 template <typename A, typename B> void is_sane(A orig)
67 {
68  std::cout << "Checking type A: " << typeid(A).name() << " converting to B: " << typeid(B).name()
69  << std::endl;
70  B converted = as<B>(orig);
71  std::cout << "Original: " << orig << ", converted: " << converted << std::endl;
72  A should_be_orig = as<A>(converted);
73  std::cout << "Converted back from B to A: " << should_be_orig << std::endl;
74  assert(should_be_orig == orig);
75  std::cout << "ok!" << std::endl;
76 }
77 
78 int main()
79 {
80  // arithmetics
81  assert(as<int>("12") == 12);
82  assert(as<int>("12.7") == std::numeric_limits<int>::max());
83  assert(as<int>("foo") == std::numeric_limits<int>::max());
84  assert(as<double>("12.7") == double(12.7));
85  assert(as<float>("12.7") == float(12.7));
86  assert(as<double>("1e3") == 1e3);
87  assert(test::isnan(as<double>("nan")));
88  assert(test::isnan(as<double>("PIG")));
89 
90  // enums
91  assert(as<MyEnum>("1") == FOO);
92  assert(as<MyEnum>(2) == BAR);
93  assert(as<MyEnum>("COW") == BAZ);
94 
95  // bool <--> string
96  assert(as<bool>("TRUE") == true);
97  assert(as<bool>("true") == true);
98  assert(as<bool>("trUe") == true);
99  assert(as<bool>("1") == true);
100  assert(as<bool>("false") == false);
101  assert(as<bool>("0") == false);
102  assert(as<bool>("DOG") == false);
103  assert(as<bool>("23") == false);
104 
105  assert(as<std::string>(true) == std::string("true"));
106  assert(as<std::string>(false) == std::string("false"));
107 
108  // class
109  assert(as<MyClass>("foobar") == MyClass());
110 
111  // two-way sanity checks
112 
113  is_sane<int, std::string>(3);
114  is_sane<double, std::string>(3.56302);
115  is_sane<float, std::string>(6.34);
116  is_sane<unsigned, std::string>(12);
117  is_sane<bool, std::string>(true);
118  is_sane<MyEnum, std::string>(BAR);
119  is_sane<MyClass, std::string>(MyClass(3, "cat"));
120 
121  std::cout << "all tests passed" << std::endl;
122 
123  return 0;
124 }
Definition: test2.pb.h:139
Definition: as.cpp:38
Definition: as.cpp:24
Definition: test_a.pb.h:42