From: Joshua Wise Date: Wed, 14 Jan 2009 07:10:56 +0000 (-0500) Subject: tests: Add the new testbench. X-Git-Url: http://git.joshuawise.com/firearm.git/commitdiff_plain/392d1bec43dcfb806fbdf77380c334f2cdc84778 tests: Add the new testbench. --- diff --git a/tests/ack.c b/tests/ack.c new file mode 100644 index 0000000..ab57018 --- /dev/null +++ b/tests/ack.c @@ -0,0 +1,25 @@ +//#test return 61 + +int ack(int m, int n) +{ + if(m == 0) { + return n + 1; + } + else if(n == 0) { + return ack(m - 1, 1); + } + else { + return ack(m - 1, ack(m, n - 1)); + } +} + +void acktest() +{ int x; + if ((x = ack(3, 3)) != 61) + { + puts("FAIL: Ack test did not return 61\n"); + puthex(x); + } + else + puts("PASS: Ack test returned 61\n"); +} diff --git a/tests/corecurse.c b/tests/corecurse.c new file mode 100644 index 0000000..33576fe --- /dev/null +++ b/tests/corecurse.c @@ -0,0 +1,25 @@ +int b(int x); +int a(int x); + +int a(int x) +{ + if (x) + return b(x/2) + a(x - 1); + return 1; +} + +int b(int x) +{ + if (x) + return a(x) + a(x - 1); + return 0; +} + +int corecurse() +{ + int v = a(35) + b(32) - 4450/28; + if (v == 15411) + puts("PASS\n"); + else + puts("FAIL\n"); +} \ No newline at end of file diff --git a/tests/j4cbo.c b/tests/j4cbo.c new file mode 100644 index 0000000..3a1382e --- /dev/null +++ b/tests/j4cbo.c @@ -0,0 +1,172 @@ +//#test return 151 + +/* example tree from http://en.wikipedia.org/wiki/Huffman_coding */ + +int getbyte(int byte) +{ + if (byte == 0) return 106; + if (byte == 1) return 139; + if (byte == 2) return 120; + if (byte == 3) return 183; + if (byte == 4) return 69; + if (byte == 5) return 197; + if (byte == 6) return 147; + if (byte == 7) return 207; + if (byte == 8) return 35; + if (byte == 9) return 155; + if (byte == 10) return 122; + if (byte == 11) return 244; + if (byte == 12) return 125; + if (byte == 13) return 215; + if (byte == 14) return 69; + if (byte == 15) return 219; + if (byte == 16) return 2; + if (byte == 17) return 224; + puts("FAIL [abort]: request for byte #"); + puthex(byte); + while(1); + return 0; +} + +int getbit(int bp) +{ + int byte; + byte = getbyte(bp/8); + return (byte >> (7 - (bp % 8))) & 1; +} + +int h(int bitpos) +{ + if (getbit(bitpos)) + return h1(bitpos+1); + else + return h0(bitpos+1); +} + +int h0(int bitpos) +{ + if (getbit(bitpos)) + return h01(bitpos+1); + else + return h00(bitpos+1); +} + +int h00(int bitpos) +{ + if (getbit(bitpos)) + return h001(bitpos+1); + else + return 69+h(bitpos+1); +} + +int h001(int bitpos) +{ + if (getbit(bitpos)) + return h0011(bitpos+1); + else + return 78+h(bitpos+1); +} + +int h0011(int bitpos) +{ + if (getbit(bitpos)) + return 79+h(bitpos+1); + else + return 85+h(bitpos+1); +} + +int h01(int bitpos) +{ + if (getbit(bitpos)) + return h011(bitpos+1); + else + return 65+h(bitpos+1); +} + +int h011(int bitpos) +{ + if (getbit(bitpos)) + return 77+h(bitpos+1); + else + return 84+h(bitpos+1); +} + +int h1(bitpos) +{ + if (getbit(bitpos)) + return h11(bitpos+1); + else + return h10(bitpos+1); +} + +int h10(bitpos) +{ + if (getbit(bitpos)) + return h101(bitpos+1); + else + return h100(bitpos+1); +} + +int h100(bitpos) +{ + if (getbit(bitpos)) + return h1001(bitpos+1); + else + return 73+h(bitpos+1); +} + +int h1001(bitpos) +{ + if (getbit(bitpos)) + return 80+h(bitpos+1); + else + return 88+h(bitpos+1); +} + +int h101(bitpos) +{ + if (getbit(bitpos)) + return h1011(bitpos+1); + else + return 72+h(bitpos+1); +} + +int h1011(bitpos) +{ + if (getbit(bitpos)) + return -2169; + else + return 83+h(bitpos+1); +} + +int h11(bitpos) +{ + if (getbit(bitpos)) + return 32+h(bitpos+1); + else + return h110(bitpos+1); +} + +int h110(bitpos) +{ + if (getbit(bitpos)) + return 70+h(bitpos+1); + else + return h1100(bitpos+1); +} + +int h1100(bitpos) +{ + if (getbit(bitpos)) + return 76+h(bitpos+1); + else + return 82+h(bitpos+1); +} + +void j4cbo() +{ + if (h(0) != 151) + puts("Result was not 151\r\n"); + else + puts("Result was 151\r\n"); +} diff --git a/tests/ram.hex b/tests/ram.hex index 0f379b7..0185b5b 120000 --- a/tests/ram.hex +++ b/tests/ram.hex @@ -1 +1 @@ -costas.hex \ No newline at end of file +testbench.hex \ No newline at end of file diff --git a/tests/testbench.c b/tests/testbench.c new file mode 100644 index 0000000..ff9f96c --- /dev/null +++ b/tests/testbench.c @@ -0,0 +1,109 @@ +extern void putc(unsigned char c); + +void puts(unsigned char *s) +{ + while (*s) + putc(*(s++)); +} + +void puthex(unsigned int x) +{ + unsigned char *hex = "0123456789ABCDEF"; + int i; + + for (i = 7; i >= 0; i--) + putc(hex[(x >> (i * 4)) & 0xF]); +} + +#define putchar putc +#include "ack.c" +#include "j4cbo.c" +#include "corecurse.c" + +int fact(int n) +{ + if (n == 0) + return 1; + else + return n * fact(n-1); +} + +void facttest() +{ + if (fact(10) != 3628800) + puts("FAIL\n"); + else + puts("PASS\n"); +} + +struct tests { + char *name; + void (*test)(); +}; + +extern int ldm_bonehead(); + +__asm__( +".globl ldm_bonehead\n" +"ldm_bonehead:;" +"mov r3, lr;" +"bl 1f;" +"nop;" +"nop;" +"nop;" +"nop;" +"nop;" +"nop;" +"nop;" +"nop;" +"nop;" +"mov pc, r3\n;" +"1:\n" +"ldr r2, =0x00002FE0;" +"ldr r1, =0x0000004C;" +"mov ip, sp;" +"stmdb sp!, {fp, ip, lr, pc};" +"mov r0, #0x00880000;" +"ldmia sp, {fp, sp, pc};" +"mul r0, r1, r2;" +"nop;" +"nop;\n" +); + +void ldm_tester() +{ + int x = ldm_bonehead(); + if (x != 0x00880000) + { + puts("FAIL: result was "); + puthex(x); + puts("\n"); + } else + puts("PASS\n"); +} + +struct tests tlist[] = { + {"ldm pc/mul", ldm_tester}, + {"fact", facttest}, + {"j4cbo", j4cbo}, + {"ack", acktest}, + {"corecurse", corecurse}, + {0, 0}}; + +int main() +{ + struct tests *t; + + puts("Testbench running\n"); + + for (t = tlist; t->name; t++) + { + puts("Running "); + puts(t->name); + puts(": "); + t->test(); + } + puts("Done!\n"); + + return 0; +} diff --git a/tests/testbench.hex b/tests/testbench.hex new file mode 100644 index 0000000..ceab6f8 Binary files /dev/null and b/tests/testbench.hex differ