Mercurial > thymian
comparison tests/var_def.cpp @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 #include "tests.h" | |
| 2 #include "gtest/gtest.h" | |
| 3 | |
| 4 /* | |
| 5 * Test for handling various variable definitions | |
| 6 */ | |
| 7 | |
| 8 /* Define a one dim array, set the first element to a value, and see that it | |
| 9 is as expected */ | |
| 10 TEST(VariableDefinitions, SimpleIndexedOperationInt) | |
| 11 { | |
| 12 SCRIPT_START | |
| 13 " \ | |
| 14 int a[10]; \ | |
| 15 a[1] = 2; \ | |
| 16 int b = a[1]; \ | |
| 17 " | |
| 18 SCRIPT_END | |
| 19 | |
| 20 ASSERT_EQ(2, VAR_INT(b)); | |
| 21 | |
| 22 SCRIPT_SHUTDOWN; | |
| 23 } | |
| 24 | |
| 25 /* Define an int variable as a substring */ | |
| 26 TEST(VariableDefinitions, IntFromSubstring) | |
| 27 { | |
| 28 SCRIPT_START | |
| 29 " \ | |
| 30 string a = \"12456789\"; \ | |
| 31 int t = a[1,3]; \ | |
| 32 int t2 = a[3,4]; \ | |
| 33 int t3 = a[3]; \ | |
| 34 int t4 = a[3, a.len - 1]; \ | |
| 35 " | |
| 36 SCRIPT_END | |
| 37 | |
| 38 ASSERT_EQ(245, VAR_INT(t)); | |
| 39 ASSERT_EQ(56, VAR_INT(t2)); | |
| 40 ASSERT_EQ(5, VAR_INT(t3)); | |
| 41 ASSERT_EQ(56789, VAR_INT(t4)); | |
| 42 SCRIPT_SHUTDOWN | |
| 43 } | |
| 44 | |
| 45 /* Define a one dim array, set the first element to a value, and see that it | |
| 46 is as expected */ | |
| 47 TEST(VariableDefinitions, SimpleIndexedOperationByte) | |
| 48 { | |
| 49 SCRIPT_START | |
| 50 " \ | |
| 51 byte a[10]; \ | |
| 52 a[1] = 2; \ | |
| 53 byte b = a[1]; \ | |
| 54 " | |
| 55 SCRIPT_END | |
| 56 | |
| 57 ASSERT_EQ(2, VAR_BYTE(b)); | |
| 58 | |
| 59 SCRIPT_SHUTDOWN; | |
| 60 } | |
| 61 | |
| 62 TEST(VariableDefinitions, ComplicatedIndexedOperationInt) | |
| 63 { | |
| 64 SCRIPT_START | |
| 65 " \ | |
| 66 int[] fun() \ | |
| 67 { \ | |
| 68 int t = 12; \ | |
| 69 int result[t]; \ | |
| 70 result[0] = 9; \ | |
| 71 return result; \ | |
| 72 } \ | |
| 73 int b[] =fun(); \ | |
| 74 int a = b[0]; \ | |
| 75 " | |
| 76 SCRIPT_END | |
| 77 | |
| 78 ASSERT_EQ(9, VAR_INT(a)); | |
| 79 | |
| 80 SCRIPT_SHUTDOWN; | |
| 81 } | |
| 82 | |
| 83 /* Define a simple integer type variable, assign a value to it. */ | |
| 84 TEST(VariableDefinitions, SimpleInt) | |
| 85 { | |
| 86 SCRIPT_START | |
| 87 " \ | |
| 88 int a; \ | |
| 89 a = 2; \ | |
| 90 " | |
| 91 SCRIPT_END | |
| 92 | |
| 93 ASSERT_EQ(2, VAR_INT(a)); | |
| 94 | |
| 95 SCRIPT_SHUTDOWN; | |
| 96 } | |
| 97 | |
| 98 | |
| 99 /* Define a simple byte type variable, assign a value to it. */ | |
| 100 TEST(VariableDefinitions, SimpleByte) | |
| 101 { | |
| 102 SCRIPT_START | |
| 103 " \ | |
| 104 byte a; \ | |
| 105 a = 2; \ | |
| 106 " | |
| 107 SCRIPT_END | |
| 108 | |
| 109 ASSERT_EQ(2, VAR_BYTE(a)); | |
| 110 | |
| 111 SCRIPT_SHUTDOWN; | |
| 112 } | |
| 113 | |
| 114 /* Define a string variable. Use the [] operator to change the second | |
| 115 character in it.*/ | |
| 116 TEST(VariableDefinitions, StringIndexedOperation) | |
| 117 { | |
| 118 SCRIPT_START | |
| 119 " \ | |
| 120 string b = \"AABB\"; \ | |
| 121 b[1] = \"c\"; \ | |
| 122 " | |
| 123 SCRIPT_END | |
| 124 | |
| 125 SCRIPT_ASSERT_STREQ("AcBB", b); | |
| 126 | |
| 127 SCRIPT_SHUTDOWN; | |
| 128 } | |
| 129 | |
| 130 /* Define a string variable, use the [,] operator to change a part from it. | |
| 131 The second indexe should be greater than the first one */ | |
| 132 TEST(VariableDefinitions, StringSubstringIndexedOperation1) | |
| 133 { | |
| 134 SCRIPT_START | |
| 135 " \ | |
| 136 string b = \"AABB\"; \ | |
| 137 b[1,2] = \"cc\"; \ | |
| 138 " | |
| 139 SCRIPT_END | |
| 140 | |
| 141 SCRIPT_ASSERT_STREQ("AccB", b); | |
| 142 | |
| 143 SCRIPT_SHUTDOWN; | |
| 144 } | |
| 145 | |
| 146 /* Define a string variable, use the [,] operator to change a part of it. | |
| 147 The second index should be greater than the length of the string. | |
| 148 Expected outcome is that the end of the string will be removed and | |
| 149 it will end with the new string. */ | |
| 150 TEST(VariableDefinitions, StringSubstringIndexedOperation2) | |
| 151 { | |
| 152 SCRIPT_START | |
| 153 " \ | |
| 154 string b = \"AABB\"; \ | |
| 155 b[1,5] = \"cc\"; \ | |
| 156 " | |
| 157 SCRIPT_END | |
| 158 | |
| 159 SCRIPT_ASSERT_STREQ("Acc", b); | |
| 160 | |
| 161 SCRIPT_SHUTDOWN; | |
| 162 } | |
| 163 | |
| 164 /* Define a string and use the substring operation on it to set the characters | |
| 165 between [x,y] to a specific string*/ | |
| 166 TEST(VariableDefinitions, StringSubstringIndexedOperation3) | |
| 167 { | |
| 168 SCRIPT_START | |
| 169 " \ | |
| 170 string b = \"AABB\"; \ | |
| 171 b[1,3] = \"cc\"; \ | |
| 172 " | |
| 173 SCRIPT_END | |
| 174 | |
| 175 SCRIPT_ASSERT_STREQ("Acc", b); | |
| 176 | |
| 177 SCRIPT_SHUTDOWN; | |
| 178 } | |
| 179 | |
| 180 TEST(VariableDefinitions, StringSubstringIndexedOperationInsertion) | |
| 181 { | |
| 182 SCRIPT_START | |
| 183 " \ | |
| 184 string b = \"ABCD\"; \ | |
| 185 b[1,1] = \"cc\"; \ | |
| 186 " | |
| 187 SCRIPT_END | |
| 188 | |
| 189 SCRIPT_ASSERT_STREQ("AccCD", b); | |
| 190 | |
| 191 SCRIPT_SHUTDOWN; | |
| 192 } | |
| 193 | |
| 194 | |
| 195 TEST(VariableDefinitions, UsingVarFromAboveScope) | |
| 196 { | |
| 197 SCRIPT_START | |
| 198 " \ | |
| 199 int b; \ | |
| 200 for(int i=0; i<10; i++) \ | |
| 201 { \ | |
| 202 int a; \ | |
| 203 a = a + i; \ | |
| 204 b += a; \ | |
| 205 } \ | |
| 206 " | |
| 207 SCRIPT_END | |
| 208 | |
| 209 ASSERT_EQ(1+2+3+4+5+6+7+8+9, VAR_INT(b)); | |
| 210 | |
| 211 SCRIPT_SHUTDOWN; | |
| 212 } | |
| 213 |
