1 #include <iostream>
2 #include "testlib/testlib_test.h"
3 #include <brdb/brdb_tuple.h>
4 #include <brdb/brdb_tuple_sptr.h>
5 #include <brdb/brdb_value.h>
6 #ifdef _MSC_VER
7 #  include "vcl_msvc_warnings.h"
8 #endif
9 
10 
test_tuple()11 static void test_tuple()
12 {
13   brdb_tuple_sptr tup0 = new brdb_tuple();
14   brdb_tuple_sptr tup1 = new brdb_tuple(10);
15   brdb_tuple_sptr tup2 = new brdb_tuple(10, 11.4f);
16   brdb_tuple_sptr tup3 = new brdb_tuple(-12.43f, std::string("this is a string"), -1 );
17   brdb_tuple_sptr tup4 = new brdb_tuple(-12.43f, 200, std::string("some text"), -1 );
18 
19   // pass if it made it this far
20   TEST("Constructors", true, true);
21 
22   TEST("arity()",tup0->arity()==0 && tup1->arity()==1 && tup2->arity()==2 &&
23                  tup3->arity()==3 && tup4->arity()==4, true);
24 
25   brdb_tuple tup_cp;
26   tup_cp = (*tup3);
27 
28   TEST("operator =", tup_cp.arity(), tup3->arity());
29 
30   int int_val = 0;
31   TEST("get() right type", tup1->get(0,int_val) && int_val==10, true);
32   TEST("get() bad bounds", tup2->get(2,int_val), false);
33 
34   TEST("set() right type", tup1->set(0, 9) && tup1->get(0,int_val) && int_val==9, true);
35   TEST("set() bad bounds", tup2->set(2, 9), false);
36 
37   brdb_value_t<std::string> new_value_string("hello");
38   std::string new_string("hello");
39   std::string get_string1;
40   std::string get_string2;
41   tup1->add_value(new_value_string);
42   tup1->get(tup1->arity()-1, get_string1);
43   tup2->add(new_string);
44   tup2->get(tup2->arity()-1, get_string2);
45   TEST("add_value()", (get_string1 == "hello"), true);
46   TEST("add()", (get_string2 == "hello"), true);
47 
48   tup1->print();
49   tup2->print();
50   TEST("print()", true, true);
51 
52 
53   //////////////////////////////////////////////////////////////////
54   //// test prototyping for binary I/O
55   //////////////////////////////////////////////////////////////////
56   brdb_tuple_sptr out_tup = new brdb_tuple(12, -12.43f, 34.56, static_cast<long>(987654321), std::string("this is a string"), false);
57   std::vector<std::string> types;
58   std::cout << "test tuple types\n";
59   for (unsigned int i=0; i<out_tup->arity(); ++i) {
60     types.push_back((*out_tup)[i].is_a());
61     std::cout << "  " << types.back() << '\n';
62   }
63   std::cout << std::flush;
64 
65   brdb_tuple_sptr in_tup = brdb_tuple::make_prototype(types);
66 
67   bool type_check = (out_tup->arity() == in_tup->arity());
68   for (unsigned int i=0; type_check && i<out_tup->arity(); ++i) {
69     type_check = (types[i] == (*in_tup)[i].is_a());
70   }
71   TEST("make_prototype()", type_check, true);
72   if (!type_check) {
73     std::cout << "mismatched prototype tuple types\n";
74     for (unsigned int i=0; type_check && i<out_tup->arity(); ++i) {
75       std::cout << "  " << (*in_tup)[i].is_a() << '\n';
76     }
77     std::cout << std::flush;
78   }
79 
80   //////////////////////////////////////////////////////////////////
81   //// test binary io on regular data types
82   //////////////////////////////////////////////////////////////////
83 
84   std::cout << "Warning: Binary I/O test deactivated because of failures" <<std::endl;
85 #if 0
86   std::cout << "out_tup: ";
87   out_tup->print();
88 
89   std::cout << "in_tup before b_read: ";
90   in_tup->print();
91 
92   vsl_b_ofstream out_stream("test_tuple_bio.vsl");
93   if (!out_stream){
94     std::cerr<<"Failed to open test_tuple_bio.vsl for output.\n";
95   }
96   std::cout << "Opened file successfully " << std::endl;
97 
98   out_tup->b_write_values(out_stream);
99   out_stream.close();
100 
101 
102   vsl_b_ifstream in_stream("test_tuple_bio.vsl");
103   if (!out_stream){
104     std::cerr<<"Failed to open test_tuple_bio.vsl for input.\n";
105   }
106   std::cout << "Opened file successfully " << std::endl;
107   in_tup->b_read_values(in_stream);
108   in_stream.close();
109 
110   std::cout << "in_tup after b_read: ";
111   in_tup->print();
112 
113   bool val_check = true;
114   for (unsigned int i=0; val_check && i<in_tup->arity(); ++i) {
115     type_check = ((*out_tup)[i] == (*in_tup)[i]);
116   }
117 
118   TEST("binary io: b_read, b_write", type_check, true);
119 #endif
120 }
121 
122 TESTMAIN(test_tuple);
123