edits
[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         Table * t1 = new Table(new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"), new IoTString("reallysecret"), 321, -1);
16         t1->initTable();
17         printf("T1 Ready\n");
18
19         Table * t2 = new Table(new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"), new IoTString("reallysecret"), 351, -1);
20         t2->update();
21         printf("T2 Ready\n");
22
23         // Make the Keys
24         printf("Setting up keys\n");
25         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
26                 printf("%d\n",i);
27                 char buffer[80];
28                 sprintf(buffer, "a%d", i);
29                 IoTString *ia = new IoTString(buffer);
30                 sprintf(buffer, "b%d", i);
31                 IoTString *ib = new IoTString(buffer);
32                 sprintf(buffer, "c%d", i);
33                 IoTString *ic = new IoTString(buffer);
34                 sprintf(buffer, "d%d", i);
35                 IoTString *id = new IoTString(buffer);
36                 t1->createNewKey(ia, 321);
37                 t1->createNewKey(ib, 351);
38                 t2->createNewKey(ic, 321);
39                 t2->createNewKey(id, 351);
40         }
41         
42         // Do Updates for the keys
43         printf("Setting Key-Values...\n");
44         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
45                 printf("%d\n", i);
46                 char buffer[80];
47                 sprintf(buffer, "a%d", i);
48                 IoTString * iKeyA = new IoTString(buffer);
49                 IoTString * iValueA = new IoTString(buffer);
50
51                 sprintf(buffer, "b%d", i);
52                 IoTString * iKeyB = new IoTString(buffer);
53                 IoTString * iValueB = new IoTString(buffer);
54                 
55                 sprintf(buffer, "c%d", i);
56                 IoTString * iKeyC = new IoTString(buffer);
57                 IoTString * iValueC = new IoTString(buffer);
58                 
59                 sprintf(buffer, "d%d", i);
60                 IoTString * iKeyD = new IoTString(buffer);
61                 IoTString * iValueD = new IoTString(buffer);
62
63                 t1->startTransaction();
64                 t1->addKV(iKeyA, iValueA);
65                 transStatusList->add(t1->commitTransaction());
66                 t1->startTransaction();
67                 t1->addKV(iKeyB, iValueB);
68                 transStatusList->add(t1->commitTransaction());
69                 
70                 t2->startTransaction();
71                 t2->addKV(iKeyC, iValueC);
72                 transStatusList->add(t2->commitTransaction());
73                 
74                 t2->startTransaction();
75                 t2->addKV(iKeyD, iValueD);
76                 transStatusList->add(t2->commitTransaction());
77         }
78         printf("Updating Clients...\n");
79         t1->update();
80         t2->update();
81         
82         printf("Checking Key-Values...\n");
83         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
84                 char buffer[80];
85                 sprintf(buffer, "a%d", i);
86                 IoTString * iKeyA = new IoTString(buffer);
87                 IoTString * iValueA = new IoTString(buffer);
88
89                 sprintf(buffer, "b%d", i);
90                 IoTString * iKeyB = new IoTString(buffer);
91                 IoTString * iValueB = new IoTString(buffer);
92                 
93                 sprintf(buffer, "c%d", i);
94                 IoTString * iKeyC = new IoTString(buffer);
95                 IoTString * iValueC = new IoTString(buffer);
96                 
97                 sprintf(buffer, "d%d", i);
98                 IoTString * iKeyD = new IoTString(buffer);
99                 IoTString * iValueD = new IoTString(buffer);
100                 
101                 IoTString *testValA1 = t1->getCommitted(iKeyA);
102                 IoTString *testValB1 = t1->getCommitted(iKeyB);
103                 IoTString *testValC1 = t1->getCommitted(iKeyC);
104                 IoTString *testValD1 = t1->getCommitted(iKeyD);
105                 
106                 IoTString *testValA2 = t2->getCommitted(iKeyA);
107                 IoTString *testValB2 = t2->getCommitted(iKeyB);
108                 IoTString *testValC2 = t2->getCommitted(iKeyC);
109                 IoTString *testValD2 = t2->getCommitted(iKeyD);
110                 
111                 if ((testValA1 == NULL) || (testValA1->equals(iValueA) == false)) {
112                         printf("Key-Value t1 incorrect: keyA\n");
113                         foundError = true;
114                 }
115                 
116                 if ((testValB1 == NULL) || (testValB1->equals(iValueB) == false)) {
117                         printf("Key-Value t1 incorrect: keyB\n");
118                         foundError = true;
119                 }
120
121                 if ((testValC1 == NULL) || (testValC1->equals(iValueC) == false)) {
122                         printf("Key-Value t1 incorrect: keyC\n");
123                         foundError = true;
124                 }
125                 
126                 if ((testValD1 == NULL) || (testValD1->equals(iValueD) == false)) {
127                         printf("Key-Value t1 incorrect: keyD\n");
128                         foundError = true;
129                 }
130                 
131                 if ((testValA2 == NULL) || (testValA2->equals(iValueA) == false)) {
132                         printf("Key-Value t2 incorrect: keyA     testValA2\n");
133                         foundError = true;
134                 }
135                 
136                 if ((testValB2 == NULL) || (testValB2->equals(iValueB) == false)) {
137                         printf("Key-Value t2 incorrect: keyB     testValB2\n");
138                         foundError = true;
139                 }
140                 
141                 if ((testValC2 == NULL) || (testValC2->equals(iValueC) == false)) {
142                         printf("Key-Value t2 incorrect: keyC     testValC2\n");
143                         foundError = true;
144                 }
145                 
146                 if ((testValD2 == NULL) || (testValD2->equals(iValueD) == false)) {
147                         printf("Key-Value t2 incorrect: keyD     testValD2\n");
148                         foundError = true;
149                 }
150         }
151
152         for (uint i = 0; i < transStatusList->size(); i++) {
153                 TransactionStatus * status = transStatusList->get(i);
154                 if (status->getStatus() != TransactionStatus_StatusCommitted) {
155                         foundError = true;
156                         printf("Status error\n");
157                 }
158         }
159         
160         if (foundError) {
161                 printf("Found Errors...\n");
162         } else {
163                 printf("No Errors Found...\n");
164         }
165 }
166