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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Nickle Testing Suite v1.0
* Operator Testing Code
* Written by Erin Chapman
*/
void check (poly value, poly correct)
{
if (is_number (value) && !is_rational (value))
{
real error = abs (value - correct);
if (error < abs(value) / 1e7)
return;
}
if (value != correct)
abort ("check failed (was %v, should be %v)", value, correct);
}
/* Assigns a value to x for testing. */
int x;
check (x = 1, 1);
/* Tests unary increment and decrememnt. */
check (++x, 2);
check (x++, 2);
check (x, 3);
check (--x, 2);
check (x--, 2);
check (x, 1);
/* Tests Unary negate */
check (-x, -1);
/* Tests logical negation */
check (!true, false);
/* Assigns a different value to x for testing. */
check (x = 4, 4);
/* Test Factorial. */
check (x!, 24);
/* Tests pointer construction and dereference. */
*int z;
check (z = &x, &x);
check (*z, 4);
/* Tests construction and evaluation of a union. */
union {int a; string b;} test;
check (sprintf ("%v", test), "{<unset>}")
check (test.a = 1, 1);
check (test, (union {int a;}) { .a = 1});
check (test.b = "hello", "hello");
check (test, (union {string b;}) { .b = "hello" });
check (bool func () { try {
test.a;
return false;
} catch invalid_struct_member (poly test, string name) {
return true;
} return false; } (), true);
/* Tests exponentiation. */
check (x**2, 16);
check (x**.5, 2);
check ((x/3)**2, 16/9);
check ((x/3)**.5, 2 / sqrt (3));
check ((-x)**2, 16);
check (bool func () { try {
(-x)**.5;
return false;
} catch invalid_argument (msg, int i, poly value) {
return true;
} return false; } (), true);
/* Tests multiplication. */
check (x*2, 8);
/* Tests division. */
check (x/2, 2);
/* Tests integer division. */
check (x//3, 1);
check (x//3 == floor(x/3), true);
check ((x//3)*3+(x%3) == x, true);
/* Tests remainder. */
check (x%3, 1);
/* Tests addition and subtraction. */
check (x+3, 7);
check (x-3, 1);
/* Tests bitwise left shift. */
check (x<<1, 8);
check (x<<-1, 2);
check (x<<1 == x*2**1, true);
/* Tests bitwise right shift. */
check (x>>1, 2);
check (x>>-1, 8);
check (x>>1 == x//2**1, true);
/* Tests relational operators. */
check (x<=10, true);
check (x>=-10, true);
check (x < 15, true);
check (x > -15, true);
/* Tests equality operators. */
check (x == x, true);
check (x != x - 1, true);
/* Tests bitwise AND. */
check (1&2, 0);
check (2&3, 2);
check (3&3, 3);
/* Tests bitwsie XOR. */
check (1^2, 3);
check (2^3, 1);
check (3^3, 0);
/* Tests bitwise OR. */
check (0|0, 0);
check (0|1, 1);
/* Tests short-circuit logical AND. */
check (true&&false, false);
check (true&&true, true);
check (false&&false, false);
check (false&&true, false);
/* Tests short-circuit logical OR. */
check (true||true, true);
check (true||false, true);
check (false||false, false);
check (false||true, true);
/* Assigns a value to y for testing. */
int y = 0;
check (y, 0);
/* Tests conditional expressions. */
check (y == 0 ? 1 : 2, 1);
check (y == 1 ? 1 : 2, 2);
/* Tests assignment operators. */
check (bool func () { try {
x = 3;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Equals assignment failed (x = 3).\n");
return false;
} return false; } (), true);
check (x, 3);
check (bool func () { try {
x += 1;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Plus equals assignment failed (x += 1).\n");
return false;
} return false; } (), true);
check (x, 4);
check (bool func () { try {
x -= 1;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Minus equals assignment failed (x -= 1).\n");
return false;
} return false; } (), true);
check (x, 3);
check (bool func () { try {
x *= 2;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Times equals assignment failed (x *= 2).\n");
return false;
} return false; } (), true);
check (x, 6);
check (bool func () { try {
x /= 2;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Divide equals assignment failed (x /= 2).\n");
return false;
} return false; } (), true);
check (x, 3);
check (bool func () { try {
x //= 2;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Integer divide equals assignment failed (x //= 2).\n");
return false;
} return false; } (), true);
check (x, 1);
check (bool func () { try {
x %= 3;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Remainder equals assignment failed (x %= 3).\n");
return false;
} return false; } (), true);
check (x, 1);
check (x = 2, 2);
check (bool func () { try {
x **= 2;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Exponentiation equals assignment failed (x **= 2).\n");
return false;
} return false; } (), true);
check (x, 4);
check (bool func () { try {
x <<= 1;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Bitwise left shift equals assignment failed (x <<= 1).\n");
return false;
} return false; } (), true);
check (x, 8);
check (bool func () { try {
x >>= 1;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Bitwise right shift equals assignment failed (x >>= 1).\n");
return false;
} return false; } (), true);
check (x, 4);
check (bool func () { try {
x ^= 2;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Bitwise XOR equals assignment failed (x ^= 2).\n");
return false;
} return false; } (), true);
check (x, 6);
check (bool func () { try {
x &= 2;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Bitwise AND equals assignment failed (x &= 2).\n");
return false;
} return false; } (), true);
check (x, 2);
check (bool func () { try {
x |= 2;
return true;
} catch invalid_argument (string msg, int i, poly value) {
printf("Bitwise OR assignment failed (x |= 2).\n");
return false;
} return false; } (), true);
check (x, 2);
quit
|