#include /* functions defined in func.c */ int mult10(int n); int rotateRight(int n, int dist); int bitSetGet(int *bits, int which); int hasOddBitCount(int n); int canFit(int n, int count); /* functions defined in this file */ void testMult10(); void testRotateRight(); void testBitSetGet(); void testHasOddBitCount(); void testCanFit(); int main() { printf("\n"); testMult10(); testRotateRight(); testBitSetGet(); testHasOddBitCount(); testCanFit(); return 0; } void testMult10() { #define NUM_MULT_10_TEST 10 struct { int parm; int result; } tests[NUM_MULT_10_TEST] = { { 0, 0 }, { 1, 10 }, { 2, 20 }, { 3, 30 }, { 4, 40 }, { 5, 50 }, { 6, 60 }, { 128, 1280 }, { 255, 2550 }, { 0x08888888, 0x55555550 }, }; int i; int parm; int want; int got; for(i = 0; i < NUM_MULT_10_TEST; i++) { parm = tests[i].parm; want = tests[i].result; got = mult10(parm); if(got != want) { printf("mult10 failed with parameter %d [0x%x]\n", parm, parm); printf(" expected: %d [0x%x]\n", want, want); printf(" got: %d [0x%x]\n", got, got); printf("mult10 testing aborted\n\n"); return; } } printf("mult10 tested OK\n\n"); } void testRotateRight() { #define NUM_ROTATE_RIGHT_TEST 24 struct { int n; int dist; int result; } tests[NUM_ROTATE_RIGHT_TEST] = { { 2, 1, 1 }, { 1, 1, 0x80000000 }, { 1, 2, 0x40000000 }, { 1, 3, 0x20000000 }, { 1, 29, 8 }, { 1, 30, 4 }, { 1, 31, 2 }, { 2, 2, 0x80000000 }, { 4, 3, 0x80000000 }, { 5, 1, 0x80000002 }, { 5, 2, 0x40000001 }, { 5, 3, 0xA0000000 }, { 5, 4, 0x50000000 }, { -1, 1, -1 }, { -1, 8, -1 }, { -1, 31, -1 }, { 0x87654321, 4, 0x18765432 }, { 0x87654321, 28, 0x76543218 }, { 0x12345678, 4, 0x81234567 }, { 0x12345678, 28, 0x23456781 }, { 0xFFFFFFFE, 1, 0x7FFFFFFF }, { 0x55555555, 1, 0xAAAAAAAA }, { 0x55555555, 2, 0x55555555 }, { 0x55555555, 4, 0x55555555 }, }; int i; int n; int dist; int got; int want; for(i = 0; i < NUM_ROTATE_RIGHT_TEST; i++) { n = tests[i].n; dist = tests[i].dist; want = tests[i].result; got = rotateRight(n, dist); if(got != want) { printf("rotateRight(0x%08x, %d) failed\n", n, dist); printf(" expected: 0x%08x\n", want); printf(" got: 0x%08x\n", got); printf("rotateRight testing aborted\n\n"); return; } } printf("rotateRight tested OK\n\n"); } void testBitSetGet() { #define NUM_BIT_SET_GET_TEST 27 struct { int bits[4]; int which; int result; } tests[NUM_BIT_SET_GET_TEST] = { { { 1, 2, 4, 8 }, 0, 1 }, { { 1, 2, 4, 8 }, 1, 0 }, { { 1, 2, 4, 8 }, 31, 0 }, { { 1, 2, 4, 8 }, 32, 0 }, { { 1, 2, 4, 8 }, 33, 1 }, { { 1, 2, 4, 8 }, 34, 0 }, { { 1, 2, 4, 8 }, 64, 0 }, { { 1, 2, 4, 8 }, 65, 0 }, { { 1, 2, 4, 8 }, 66, 1 }, { { 1, 2, 4, 8 }, 67, 0 }, { { 1, 2, 4, 8 }, 96, 0 }, { { 1, 2, 4, 8 }, 98, 0 }, { { 1, 2, 4, 8 }, 99, 1 }, { { 1, 2, 4, 8 }, 100, 0 }, { { -1, -2, -4, -8 }, 1, 1 }, { { -1, -2, -4, -8 }, 31, 1 }, { { -1, -2, -4, -8 }, 32, 0 }, { { -1, -2, -4, -8 }, 63, 1 }, { { -1, -2, -4, -8 }, 64, 0 }, { { -1, -2, -4, -8 }, 65, 0 }, { { -1, -2, -4, -8 }, 66, 1 }, { { -1, -2, -4, -8 }, 95, 1 }, { { -1, -2, -4, -8 }, 96, 0 }, { { -1, -2, -4, -8 }, 97, 0 }, { { -1, -2, -4, -8 }, 98, 0 }, { { -1, -2, -4, -8 }, 99, 1 }, { { -1, -2, -4, -8 }, 100, 1 }, }; int i; int *bits; int which; int got; int want; for(i = 0; i < NUM_BIT_SET_GET_TEST; i++) { bits = tests[i].bits; which = tests[i].which; want = tests[i].result; got = bitSetGet(bits, which); if(got != want) { printf("bitSetGet({%08x,%08x,%08x,%08x}, %d) failed\n", bits[0], bits[1], bits[2], bits[3], which); printf(" expected: %d\n", want); printf(" got: %d\n", got); printf("bitSetGet testing aborted\n\n"); return; } } printf("bitSetGet tested OK\n\n"); } void testHasOddBitCount() { #define NUM_HAS_ODD_BIT_COUNT_TEST 24 struct { int parm; int result; } tests[NUM_HAS_ODD_BIT_COUNT_TEST] = { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 0 }, { 4, 1 }, { 5, 0 }, { 6, 0 }, { 7, 1 }, { 8, 1 }, { 9, 0 }, { 10, 0 }, { 11, 1 }, { 12, 0 }, { 13, 1 }, { 14, 1 }, { 15, 0 }, { 16, 1 }, { 0x12345678, 1 }, { 0x12345679, 0 }, { 0x87654321, 1 }, { 0x97654321, 0 }, { 0xFFFFFFFF, 0 }, { 0xFFEFFFFF, 1 }, { 0x7FFFFFFF, 1 }, }; int i; int parm; int got; int want; for(i = 0; i < NUM_HAS_ODD_BIT_COUNT_TEST; i++) { parm = tests[i].parm; want = tests[i].result; got = hasOddBitCount(parm); if(got != want) { printf("hasOddBitCount failed with parameter %d [0x%x]\n", parm, parm); printf(" expected: %d\n", want); printf(" got: %d\n", got); printf("hasOddBitCount testing aborted\n\n"); return; } } printf("hasOddBitCount tested OK\n\n"); } void testCanFit() { #define NUM_TEST_CAN_FIT 21 struct { int parm; int count; } tests[NUM_HAS_ODD_BIT_COUNT_TEST] = { { 1, 1 }, { 2, 2 }, { 3, 2 }, { 4, 3 }, { 5, 3 }, { 6, 3 }, { 7, 3 }, { 8, 4 }, { 9, 4 }, { 10, 4 }, { 11, 4 }, { 12, 4 }, { 13, 4 }, { 14, 4 }, { 15, 4 }, { 16, 5 }, { 0x12345678, 29 }, { 0x1FFFFFFF, 29 }, { 0x20000000, 30 }, { 0x40000000, 31 }, { 0x7FFFFFFF, 31 }, }; int i; int j; int parm; int count; int want; int got; for(i = 0; i < NUM_HAS_ODD_BIT_COUNT_TEST; i++) { parm = tests[i].parm; count = tests[i].count; for(j = 1; j <= 31; j++) { want = j >= count; got = canFit(parm, j); if(got != want) { printf("canFit(0x%08x, %d) failed\n", parm, j); printf(" expected: %d\n", want); printf(" got: %d\n", got); printf("canFit testing aborted\n\n"); return; } } } printf("canFit tested OK\n\n"); }