408622caf5bdbca400bcc649afb4a3e0a0234e2a
[iotcloud.git] / version2 / src / C / Test.ino
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 TimingSingleton *timer;
9 bool foundError;
10 MyVector<TransactionStatus *> * transStatusList;
11 Table *t1;
12
13 void setup() {
14   Serial.begin();
15         timer = TimingSingleton_getInstance();
16         foundError = false;
17         transStatusList = new MyVector<TransactionStatus *>();
18         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
19   IoTString * password = new IoTString("reallysecret");
20         t1 = new Table(baseurl, password, 321, -1);
21   t1->initTable();                               
22
23         baseurl->releaseRef();
24         password->releaseRef();
25
26         // Make the Keys
27         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
28                 char buffer[80];
29                 sprintf(buffer, "a%d", i);
30                 IoTString *ia = new IoTString(buffer);
31                 sprintf(buffer, "b%d", i);
32                 IoTString *ib = new IoTString(buffer);
33                 t1->createNewKey(ia, 321);
34                 t1->createNewKey(ib, 351);
35                 ia->releaseRef();
36                 ib->releaseRef();
37         }
38 }
39
40
41 void loop() {
42         // Do Updates for the keys
43         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
44                 char buffer[80];
45                 sprintf(buffer, "a%d", i);
46                 IoTString * iKeyA = new IoTString(buffer);
47                 IoTString * iValueA = new IoTString(buffer);
48
49                 sprintf(buffer, "b%d", i);
50                 IoTString * iKeyB = new IoTString(buffer);
51                 IoTString * iValueB = new IoTString(buffer);
52
53                 t1->startTransaction();
54                 t1->put(iKeyA, iValueA);
55                 transStatusList->add(t1->commitTransaction());
56                 iKeyA->releaseRef();
57                 iValueA->releaseRef();
58
59                 t1->startTransaction();
60                 t1->put(iKeyB, iValueB);
61                 transStatusList->add(t1->commitTransaction());
62                 iKeyB->releaseRef();
63                 iValueB->releaseRef();
64         }
65
66         t1->update();
67
68         for (int i = 0; i < NUMBER_OF_TESTS; i++) {
69                 char buffer[80];
70                 sprintf(buffer, "a%d", i);
71                 IoTString * iKeyA = new IoTString(buffer);
72                 IoTString * iValueA = new IoTString(buffer);
73
74                 sprintf(buffer, "b%d", i);
75                 IoTString * iKeyB = new IoTString(buffer);
76                 IoTString * iValueB = new IoTString(buffer);
77
78                 IoTString *testValA1 = t1->getCommitted(iKeyA);
79                 IoTString *testValB1 = t1->getCommitted(iKeyB);
80
81                 if ((testValA1 == NULL) || (testValA1->equals(iValueA) == false)) {
82                 //                      printf("Key-Value t1 incorrect: keyA\n");
83                         foundError = true;
84                 }
85
86                 if ((testValB1 == NULL) || (testValB1->equals(iValueB) == false)) {
87                 //                      printf("Key-Value t1 incorrect: keyB\n");
88                         foundError = true;
89                 }
90
91                 iKeyA->releaseRef();
92                 iValueA->releaseRef();
93                 iKeyB->releaseRef();
94                 iValueB->releaseRef();
95                 testValA1->releaseRef();
96                 testValB1->releaseRef();
97         }
98
99         for (uint i = 0; i < transStatusList->size(); i++) {
100                 TransactionStatus * status = transStatusList->get(i);
101                 if (status->getStatus() != TransactionStatus_StatusCommitted) {
102                         foundError = true;
103 //                      printf("Status error\n");
104                 }
105                 delete status;
106         }
107
108         if (foundError) {
109 //              printf("Found Errors...\n");
110         } else {
111         }
112
113         delete transStatusList;
114         delete t1;
115 }