#include "smat.h" #include #include #include #include #include #include #include #include #ifdef linux #include #endif #define EXPECT_TRUE(EXPR) \ do { if (EXPR) ; else exit(EXIT_FAILURE); } while (0) short a[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; template static void cmp() { T i1(&a[0]), i2 = &a[2], i3; i3 = &a[2]; EXPECT_TRUE(i2 == i3 && i1 != i2 && !(i2 == i1 || i2 != i3)); EXPECT_TRUE(i1 < i2 && !(i2 < i1 || i2 < i3)); EXPECT_TRUE(i2 > i1 && !(i1 > i2 || i3 > i2)); EXPECT_TRUE(i1 <= i2 && i2 <= i3 && !(i2 <= i1)); EXPECT_TRUE(i2 >= i1 && i3 >= i2 && !(i1 >= i2)); } template static void deref() { T i1(&a[0]), i2(&a[1]), i3 = i1; typename T::value_type v1 = a[0], v2 = a[1]; typename T::difference_type d = 1; EXPECT_TRUE(*i1 == v1 && !(*i1 == v2)); EXPECT_TRUE(*i1++ == v1 && i1 == i2 && *i1-- == v2 && i1 == i3); EXPECT_TRUE(*++i1 == v2 && i1 == i2 && *--i1 == v1 && i1 == i3); *i1 = v1; EXPECT_TRUE(*i1 == v1); EXPECT_TRUE(i1[d] == *i2 && i1[1] == *i2); i1[1] = v2; } template static void plusminus() { T i1(&a[0]), i2(&a[5]), i3(i1); typename T::difference_type d = 5; EXPECT_TRUE(i2 == i1 + 5 && i1 + 5 == i2 && i2 == 5 + i1 && 5 + i1 == i2); EXPECT_TRUE(i2 == i1 + d && i1 + d == i2 && i2 == d + i1 && d + i1 == i2); EXPECT_TRUE(i1 == i2 - 5 && i2 - 5 == i1); EXPECT_TRUE(i1 == i2 - d && i2 - d == i1); EXPECT_TRUE(i1 == i2 + (-d) && i2 + (-d) == i1); i1 += d; EXPECT_TRUE(i1 == i2); i1 -= d; EXPECT_TRUE(i1 == i3); i1 += 5; EXPECT_TRUE(i1 == i2); i1 -= 5; EXPECT_TRUE(i1 == i3); EXPECT_TRUE(i1 && !!i1); } struct foo { int x, y; }; static void arrow() { foo z = { 1, 2 }; smat i = &z; EXPECT_TRUE(i->x == 1 && i->y == 2); } template static void arith() { T x(0),y,z(x); x = y = z = 5; x += 3; EXPECT_TRUE(x + 7 == 15 && 7 + x == 15); // x == 8 x -= 2; EXPECT_TRUE(x - 2 == 4 && 8 - x == 2); // x == 6 x *= -2; EXPECT_TRUE(x * 4 == -48 && 4 * x == -48); x *= 4; // x == -48 x /= 3; EXPECT_TRUE(x / (-2) == 8 && 100 / x == -6); x /= -2; EXPECT_TRUE(++x == 9 && x++ == 9 && x == 10); EXPECT_TRUE(--x == 9 && x-- == 9 && x == 8); x %= 5; EXPECT_TRUE(x == 3 && x % 7 == 3 && (10 % (x = 3)) == 1); x <<= 3; EXPECT_TRUE(x == 24 && (x << 2) == 96 && ((1 << (x = 6)) == 64)); x = 24; x >>= 1; EXPECT_TRUE(x == 12 && (x >> 1) == 6 && ((128 >> (x = 2)) == 32)); x = 12; x &= 0x1c; EXPECT_TRUE(x == 0xc && (x & 0x1c) == 0xc && (0x1c & x) == 0xc); x |= 0x11; EXPECT_TRUE(x == 0x1d && (x | 0x20) == 0x3d && (0x20|x) == 0x3d); x ^= 0xff; EXPECT_TRUE(x == 0xe2 && (x ^ 0xff) == 0x1d && (0xff^x) == 0x1d); EXPECT_TRUE(+y == 5 && -y == -5 && ~y == -6 && y == z && y != x); EXPECT_TRUE(x > y && y < x && x >= y && y <= x && y >= z && y <= z); } int main() { cmp >(); deref >(); plusminus >(); arrow(); cmp >(); deref >(); plusminus >(); arith >(); arith >(); }