Repository navigation
Expand file tree
/
Copy pathtestrecord.cpp
More file actions
153 lines (128 loc) · 3.89 KB
/
Copy pathtestrecord.cpp
File metadata and controls
153 lines (128 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "record.h"
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE const_string test
#include <boost/test/unit_test.hpp>
#include <cassert>
#include <fstream>
#include <sstream>
#include <string>
#include <utility>
using namespace std;
#include "utility.h"
#include "fraction.h"
template<class Value>
void ReadRecords(const char *filename, Value& val)
{
ifstream in(filename);
assert(in);
while (in.good() && in.peek() != EOF)
ReadOneRecord(in, val);
cout << "All done!" << endl;
}
BOOST_AUTO_TEST_CASE( constructors ) {
cout << "Constructors" << endl;
Record<int> rec;
BOOST_CHECK(rec.isSelected() == false);
BOOST_CHECK(rec.entries.empty());
Record<int> rec2 = Record<int>();
BOOST_CHECK(rec2.isSelected() == false);
rec2.setSelected(false);
BOOST_CHECK(rec2.isSelected() == false);
rec2.setSelected(true);
BOOST_CHECK(rec2.isSelected() == true);
cout << "finished Constructors" << endl;
}
BOOST_AUTO_TEST_CASE( operatorinInt ) {
cout << "operator>> and << int" << endl;
Record<int> rec;
ifstream in("db_int");
assert(in);
in >> rec;
cout << rec << endl;
}
BOOST_AUTO_TEST_CASE( operatorinstring) {
cout << "operator>> and << string" << endl;
Record<string> rec;
ifstream in("db_string");
assert(in);
in >> rec;
cout << rec << endl;
}
BOOST_AUTO_TEST_CASE( operatorinfract ) {
cout << "operator>> and << fract" << endl;
Record<Fraction> rec;
ifstream in("db_fract");
assert(in);
in >> rec;
cout << rec << endl;
}
BOOST_AUTO_TEST_CASE( test_matchesquery ) {
cout << "test_matchesQuery" << endl;
Record<int> rec;
cout << "stream in db_int" << endl;
ifstream in("db_int");
cout << "assering db_int" << endl;
assert(in);
cout << "reading in record" << endl;
in >> rec;
cout << "finished reading in record" << endl;
BOOST_CHECK_EQUAL(rec.matchesQuery("part number", Equal, 16485), true);
BOOST_CHECK_EQUAL(rec.matchesQuery("part number", Equal, 4599957), false);
BOOST_CHECK_EQUAL(rec.matchesQuery("part number", NotEqual, 14256485), true);
BOOST_CHECK_EQUAL(rec.matchesQuery("part number", LessThan, 14256485), true);
BOOST_CHECK_EQUAL(rec.matchesQuery("part number", GreaterThan, 43), true);
BOOST_CHECK_EQUAL(rec.matchesQuery("retail cost", GreaterThan, 43), false);
BOOST_CHECK_EQUAL(rec.matchesQuery("number in stock", GreaterThan, 5), true);
BOOST_CHECK_EQUAL(rec.matchesQuery("*", GreaterThan, 5), true);
BOOST_CHECK_EQUAL(rec.matchesQuery("*", GreaterThan, 43), true);
BOOST_CHECK_EQUAL(rec.matchesQuery("*", GreaterThan, 99999993), false);
BOOST_CHECK_EQUAL(rec.matchesQuery("*", LessThan, 999993), true);
BOOST_CHECK_EQUAL(rec.matchesQuery("*", LessThan, 3), false);
}
//int main()
//{
//cout << "What type values would you like to read in the database?\n";
//cout << "(1 = integer, 2 = string, 3 = Fraction): ";
//int choice;
//cin >> choice;
//switch (choice) {
//case 1: {
//int i;
//ReadRecords("db_int", i);
//break;
//}
//case 2: {
//string s;
//ReadRecords("db_string",s);
//break;
//}
////case 3: {
////Fraction f;
////ReadRecords("db_fract", f);
////break;
////}
//default:
//cout << "Invalid choice, exiting.\n";
//}
//return 0;
//}
template<class value>
void ReadOneRecord(istream& in, value& val)
{
if (in.peek() != '{') { // bail if not at record start
in.setstate(ios::failbit); // set stream failure, so we can detect it
return;
}
in.ignore(1024, '\n');
while (true) {
string line;
getline(in, line, '\n');
istringstream istr(line.c_str());
if (istr.peek() == '}') break;
std::pair<string, value> entry;
ReadFieldname(istr, entry.first);
ReadValue(istr, entry.second);
cout << "pair<attribute : value> = <" << entry.first << " : "
<< entry.second << ">" << endl;
}
}