Adding files to initialize, update, and read key-value (to be used together with...
[iotcloud.git] / version2 / src / C / Test.C
1 #include "Table.h"
2 #include "IoTString.h"
3 #include "TimingSingleton.h"
4 #include "TransactionStatus.h"
5
6 #define NUMBER_OF_TESTS 2
7
8 int main(int numargs, char ** args) {
9         TimingSingleton * timer = TimingSingleton_getInstance();
10
11         bool foundError = false;
12         Vector<TransactionStatus *> * transStatusList = new Vector<TransactionStatus *>();
13
14         // Setup the 2 clients
15         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
16         IoTString * password = new IoTString("reallysecret");
17         Table * t1 = new Table(baseurl, password, 321, -1);
18         t1->initTable();
19         printf("T1 Ready\n");
20
21         Table * t2 = new Table(baseurl, password, 351, -1);
22         t2->update();
23         printf("T2 Ready\n");
24
25         baseurl->releaseRef();
26         password->releaseRef();
27         
28         // Make the Keys
29         printf("Setting up keys\n");
30         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
31                 printf("%d\n",i);
32                 char buffer[80];
33                 sprintf(buffer, "a%d", i);
34                 IoTString *ia = new IoTString(buffer);
35                 sprintf(buffer, "b%d", i);
36                 IoTString *ib = new IoTString(buffer);
37                 sprintf(buffer, "c%d", i);
38                 IoTString *ic = new IoTString(buffer);
39                 sprintf(buffer, "d%d", i);
40                 IoTString *id = new IoTString(buffer);
41                 t1->createNewKey(ia, 321);
42                 t1->createNewKey(ib, 351);
43                 t2->createNewKey(ic, 321);
44                 t2->createNewKey(id, 351);
45                 ia->releaseRef();
46                 ib->releaseRef();
47                 ic->releaseRef();
48                 id->releaseRef();
49         }
50         
51         // Do Updates for the keys
52         printf("Setting Key-Values...\n");
53         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
54                 printf("%d\n", i);
55                 char buffer[80];
56                 sprintf(buffer, "a%d", i);
57                 IoTString * iKeyA = new IoTString(buffer);
58                 IoTString * iValueA = new IoTString(buffer);
59
60                 sprintf(buffer, "b%d", i);
61                 IoTString * iKeyB = new IoTString(buffer);
62                 IoTString * iValueB = new IoTString(buffer);
63                 
64                 sprintf(buffer, "c%d", i);
65                 IoTString * iKeyC = new IoTString(buffer);
66                 IoTString * iValueC = new IoTString(buffer);
67                 
68                 sprintf(buffer, "d%d", i);
69                 IoTString * iKeyD = new IoTString(buffer);
70                 IoTString * iValueD = new IoTString(buffer);
71
72                 t1->startTransaction();
73                 t1->put(iKeyA, iValueA);
74                 transStatusList->add(t1->commitTransaction());
75                 iKeyA->releaseRef();
76                 iValueA->releaseRef();
77                 
78                 t1->startTransaction();
79                 t1->put(iKeyB, iValueB);
80                 transStatusList->add(t1->commitTransaction());
81                 iKeyB->releaseRef();
82                 iValueB->releaseRef();
83                 
84                 t2->startTransaction();
85                 t2->put(iKeyC, iValueC);
86                 transStatusList->add(t2->commitTransaction());
87                 iKeyC->releaseRef();
88                 iValueC->releaseRef();
89                 
90                 t2->startTransaction();
91                 t2->put(iKeyD, iValueD);
92                 transStatusList->add(t2->commitTransaction());
93                 iKeyD->releaseRef();
94                 iValueD->releaseRef();
95         }
96         
97         printf("Updating Clients...\n");
98         t1->update();
99         t2->update();
100         
101         printf("Checking Key-Values...\n");
102         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
103                 char buffer[80];
104                 sprintf(buffer, "a%d", i);
105                 IoTString * iKeyA = new IoTString(buffer);
106                 IoTString * iValueA = new IoTString(buffer);
107
108                 sprintf(buffer, "b%d", i);
109                 IoTString * iKeyB = new IoTString(buffer);
110                 IoTString * iValueB = new IoTString(buffer);
111                 
112                 sprintf(buffer, "c%d", i);
113                 IoTString * iKeyC = new IoTString(buffer);
114                 IoTString * iValueC = new IoTString(buffer);
115                 
116                 sprintf(buffer, "d%d", i);
117                 IoTString * iKeyD = new IoTString(buffer);
118                 IoTString * iValueD = new IoTString(buffer);
119                 
120                 IoTString *testValA1 = t1->getCommitted(iKeyA);
121                 IoTString *testValB1 = t1->getCommitted(iKeyB);
122                 IoTString *testValC1 = t1->getCommitted(iKeyC);
123                 IoTString *testValD1 = t1->getCommitted(iKeyD);
124                 
125                 IoTString *testValA2 = t2->getCommitted(iKeyA);
126                 IoTString *testValB2 = t2->getCommitted(iKeyB);
127                 IoTString *testValC2 = t2->getCommitted(iKeyC);
128                 IoTString *testValD2 = t2->getCommitted(iKeyD);
129                 
130                 if ((testValA1 == NULL) || (testValA1->equals(iValueA) == false)) {
131                         printf("Key-Value t1 incorrect: keyA\n");
132                         foundError = true;
133                 }
134                 
135                 if ((testValB1 == NULL) || (testValB1->equals(iValueB) == false)) {
136                         printf("Key-Value t1 incorrect: keyB\n");
137                         foundError = true;
138                 }
139
140                 if ((testValC1 == NULL) || (testValC1->equals(iValueC) == false)) {
141                         printf("Key-Value t1 incorrect: keyC\n");
142                         foundError = true;
143                 }
144                 
145                 if ((testValD1 == NULL) || (testValD1->equals(iValueD) == false)) {
146                         printf("Key-Value t1 incorrect: keyD\n");
147                         foundError = true;
148                 }
149                 
150                 if ((testValA2 == NULL) || (testValA2->equals(iValueA) == false)) {
151                         printf("Key-Value t2 incorrect: keyA     testValA2\n");
152                         foundError = true;
153                 }
154                 
155                 if ((testValB2 == NULL) || (testValB2->equals(iValueB) == false)) {
156                         printf("Key-Value t2 incorrect: keyB     testValB2\n");
157                         foundError = true;
158                 }
159                 
160                 if ((testValC2 == NULL) || (testValC2->equals(iValueC) == false)) {
161                         printf("Key-Value t2 incorrect: keyC     testValC2\n");
162                         foundError = true;
163                 }
164                 
165                 if ((testValD2 == NULL) || (testValD2->equals(iValueD) == false)) {
166                         printf("Key-Value t2 incorrect: keyD     testValD2\n");
167                         foundError = true;
168                 }
169                 iKeyA->releaseRef();
170                 iValueA->releaseRef();
171                 iKeyB->releaseRef();
172                 iValueB->releaseRef();
173                 iKeyC->releaseRef();
174                 iValueC->releaseRef();
175                 iKeyD->releaseRef();
176                 iValueD->releaseRef();
177                 testValA1->releaseRef();
178                 testValA2->releaseRef();
179                 testValB1->releaseRef();
180                 testValB2->releaseRef();
181                 testValC1->releaseRef();
182                 testValC2->releaseRef();
183                 testValD1->releaseRef();
184                 testValD2->releaseRef();
185         }
186
187         for (uint i = 0; i < transStatusList->size(); i++) {
188                 TransactionStatus * status = transStatusList->get(i);
189                 if (status->getStatus() != TransactionStatus_StatusCommitted) {
190                         foundError = true;
191                         printf("Status error\n");
192                 }
193                 delete status;
194         }
195         
196         if (foundError) {
197                 printf("Found Errors...\n");
198         } else {
199                 printf("No Errors Found...\n");
200         }
201
202         delete transStatusList;
203         delete t1;
204 }
205