Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edadma committed Apr 5, 2018
1 parent 5fba9d2 commit 7040af1
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/scala/Instruction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ abstract class UTypeInstruction( mnemonic: String ) extends Instruction {

val rd: Int

def immediate( cpu: CPU ) = cpu.instruction&0xFFFFF000L
def immediate( cpu: CPU ) = cpu.instruction&0xFFFFFFFFFFFFF000L

def disassemble( cpu: CPU ) = s"$mnemonic x$rd, ${immediate( cpu )}"

Expand Down
25 changes: 25 additions & 0 deletions src/test/scala/ExamplesNoCompressed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ class ExamplesNoCompressed extends FreeSpec with PropertyChecks with Matchers {
""".trim.stripMargin
}

"int2stru" in {
Run( "tests/int2stru.hex" ) shouldBe
"""
|zero
|zero
|zero
|zero
""".trim.stripMargin
}

"int2str64" in {
Run( "tests/int2str64.hex" ) shouldBe
"""
|0
|123
|12AB
|2000000000
|20000000000
|-123
|-12AB
|-2000000000
|-20000000000
""".trim.stripMargin
}

"armstrong" in {
Run( "tests/armstrong.hex" ) shouldBe
"""
Expand Down
58 changes: 58 additions & 0 deletions tests/int2str64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//#include <stdio.h>
//#define out( c ) putchar( c )

void
out( char c ) {
*((char*) 0x20000) = c;
}

void
print( char* s ) {
while (*s)
out( *s++ );
}

void
println( char* s ) {
print( s );
out( '\n' );
}

char*
int2str64( long n, int radix, char* buf ) {
char digits[] = "0123456789ABCDEF";
char* p = &buf[33];
long quo = n;

if (n < 0)
quo = -quo;

*p-- = 0;

while (quo >= radix) {
*p-- = digits[(quo%radix)];
quo /= radix;
}

*p = digits[quo];

if (n < 0)
*--p = '-';

return p;
}

void
main() {
char buf[34];

println( int2str64(0, 10, buf) );
println( int2str64(123, 10, buf) );
println( int2str64(0x12AB, 16, buf) );
println( int2str64(2000000000, 10, buf) );
println( int2str64(20000000000, 10, buf) );
println( int2str64(-123, 10, buf) );
println( int2str64(-0x12AB, 16, buf) );
println( int2str64(-2000000000, 10, buf) );
println( int2str64(-20000000000, 10, buf) );
}
63 changes: 63 additions & 0 deletions tests/int2str64.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

Hex dump of section '.text':
0x00010080 37010100 ef000020 73500000 00000000 7...... sP......
0x00010090 00000000 00000000 00000000 00000000 ................
0x000100a0 00000000 00000000 00000000 00000000 ................
0x000100b0 00000000 00000000 00000000 00000000 ................
0x000100c0 130101fe 233c8100 13040102 93070500 ....#<..........
0x000100d0 a307f4fe b7070200 0347f4fe 2380e700 .........G..#...
0x000100e0 13000000 03348101 13010102 67800000 .....4......g...
0x000100f0 130101fe 233c1100 23388100 13040102 ....#<..#8......
0x00010100 2334a4fe 6f00c001 833784fe 13871700 #4..o....7......
0x00010110 2334e4fe 83c70700 13850700 eff05ffa #4............_.
0x00010120 833784fe 83c70700 e39007fe 13000000 .7..............
0x00010130 83308101 03340101 13010102 67800000 .0...4......g...
0x00010140 130101fe 233c1100 23388100 13040102 ....#<..#8......
0x00010150 2334a4fe 033584fe eff09ff9 1305a000 #4...5..........
0x00010160 eff01ff6 13000000 83308101 03340101 .........0...4..
0x00010170 13010102 67800000 130101fa 233c8104 ....g.......#<..
0x00010180 13040106 233ca4fa 93870500 2334c4fa ....#<......#4..
0x00010190 232af4fa b7070100 03b7873e 2334e4fc #*.........>#4..
0x000101a0 1387873e 03378700 2338e4fc 9387873e ...>.7..#8.....>
0x000101b0 83c70701 230cf4fc 833784fa 93871702 ....#....7......
0x000101c0 2334f4fe 833784fb 2330f4fe 833784fb #4...7..#0...7..
0x000101d0 63d80700 833704fe b307f040 2330f4fe c....7.....@#0..
0x000101e0 833784fe 1387f7ff 2334e4fe 23800700 .7......#4..#...
0x000101f0 6f00c003 832744fb 033704fe 3367f702 o....'D..7..3g..
0x00010200 833784fe 9386f7ff 2334d4fe 930604ff .7......#4......
0x00010210 3387e600 034787fd 2380e700 832744fb 3....G..#....'D.
0x00010220 033704fe b347f702 2330f4fe 832744fb .7...G..#0...'D.
0x00010230 033704fe e350f7fc 833704fe 130704ff .7...P...7......
0x00010240 b307f700 03c787fd 833784fe 2380e700 .........7..#...
0x00010250 833784fb 63de0700 833784fe 9387f7ff .7..c....7......
0x00010260 2334f4fe 833784fe 1307d002 2380e700 #4...7......#...
0x00010270 833784fe 13850700 03348105 13010106 .7.......4......
0x00010280 67800000 130101fc 233c1102 23388102 g.......#<..#8..
0x00010290 13040104 930784fc 13860700 9305a000 ................
0x000102a0 13050000 eff05fed 93070500 13850700 ......_.........
0x000102b0 eff01fe9 930784fc 13860700 9305a000 ................
0x000102c0 1305b007 eff05feb 93070500 13850700 ......_.........
0x000102d0 eff01fe7 930784fc 13860700 93050001 ................
0x000102e0 b7170000 1385b72a eff01fe9 93070500 .......*........
0x000102f0 13850700 eff0dfe4 930784fc 13860700 ................
0x00010300 9305a000 b7973577 13850740 eff0dfe6 ......5w...@....
0x00010310 93070500 13850700 eff09fe2 930784fc ................
0x00010320 13860700 9305a000 b7079500 9387972f .............../
0x00010330 1395b700 eff05fe4 93070500 13850700 ......_.........
0x00010340 eff01fe0 930784fc 13860700 9305a000 ................
0x00010350 130550f8 eff05fe2 93070500 13850700 ..P..._.........
0x00010360 eff01fde 930784fc 13860700 93050001 ................
0x00010370 b7f7ffff 138557d5 eff01fe0 93070500 ......W.........
0x00010380 13850700 eff0dfdb 930784fc 13860700 ................
0x00010390 9305a000 b777ca88 138507c0 eff0dfdd .....w..........
0x000103a0 93070500 13850700 eff09fd9 930784fc ................
0x000103b0 13860700 9305a000 b7076bff 938777d0 ..........k...w.
0x000103c0 1395b700 eff05fdb 93070500 13850700 ......_.........
0x000103d0 eff01fd7 13000000 83308103 03340103 .........0...4..
0x000103e0 13010104 67800000 ....g...


Hex dump of section '.rodata':
0x000103e8 30313233 34353637 38394142 43444546 0123456789ABCDEF
0x000103f8 00 .

71 changes: 71 additions & 0 deletions tests/int2stru.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
void
out( char c ) {
*((char*) 0x20000) = c;
}

void
print( char* s ) {
while (*s)
out( *s++ );

out( '\n' );
}

int
signum( int n ) {
return n == 0 ? 0 : n < 0 ? -1 : 1;
}

void
prints( int n ) {
static char* signs[] = {
"negative",
"zero",
"positive"
};

print( signs[signum(n) + 1] );
}

char*
int2stru( unsigned int n, int radix, char* buf ) {
char digits[] = "0123456789ABCDEF";
char* p = &buf[33];
unsigned int quo = n;

*p-- = 0;

while (quo >= radix) {
*p-- = digits[(quo%radix)];
quo /= radix;
}

*p = digits[quo];

return p;
}

int
strcmp( char *s1, char *s2 ) {
unsigned char c1, c2;

do {
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;

if (c1 == '\0')
return c1 - c2;
} while (c1 == c2);

return c1 - c2;
}

void
main() {
char buf[34];

prints( strcmp(int2stru(0, 10, buf), "0") );
prints( strcmp(int2stru(123, 10, buf), "123") );
prints( strcmp(int2stru(0x12AB, 16, buf), "12AB") );
prints( strcmp(int2stru(0xF0000000, 16, buf), "F0000000") );
}
82 changes: 82 additions & 0 deletions tests/int2stru.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

Hex dump of section '.text':
0x000100c0 37010100 ef004030 73500000 00000000 7.....@0sP......
0x000100d0 00000000 00000000 00000000 00000000 ................
0x000100e0 00000000 00000000 00000000 00000000 ................
0x000100f0 00000000 00000000 00000000 00000000 ................
0x00010100 130101fe 233c8100 13040102 93070500 ....#<..........
0x00010110 a307f4fe b7070200 0347f4fe 2380e700 .........G..#...
0x00010120 13000000 03348101 13010102 67800000 .....4......g...
0x00010130 130101fe 233c1100 23388100 13040102 ....#<..#8......
0x00010140 2334a4fe 6f00c001 833784fe 13871700 #4..o....7......
0x00010150 2334e4fe 83c70700 13850700 eff05ffa #4............_.
0x00010160 833784fe 83c70700 e39007fe 1305a000 .7..............
0x00010170 eff01ff9 13000000 83308101 03340101 .........0...4..
0x00010180 13010102 67800000 130101fe 233c8100 ....g.......#<..
0x00010190 13040102 93070500 2326f4fe 8327c4fe ........#&...'..
0x000101a0 9b870700 63800702 8327c4fe 9b870700 ....c....'......
0x000101b0 63d60700 9307f0ff 6f000001 93071000 c.......o.......
0x000101c0 6f008000 93070000 13850700 03348101 o............4..
0x000101d0 13010102 67800000 130101fe 233c1100 ....g.......#<..
0x000101e0 23388100 13040102 93070500 2326f4fe #8..........#&..
0x000101f0 8327c4fe 13850700 eff01ff9 93070500 .'..............
0x00010200 9b871700 1b870700 b7170100 13173700 ..............7.
0x00010210 93878752 b307f700 83b70700 13850700 ...R............
0x00010220 eff01ff1 13000000 83308101 03340101 .........0...4..
0x00010230 13010102 67800000 130101fc 233c8102 ....g.......#<..
0x00010240 13040104 93070500 13870500 2330c4fc ............#0..
0x00010250 2326f4fc 93070700 2324f4fc b7070100 #&......#$......
0x00010260 03b7074c 2338e4fc 1387074c 03378700 ...L#8.....L.7..
0x00010270 233ce4fc 9387074c 83c70701 2300f4fe #<.....L....#...
0x00010280 833704fc 93871702 2334f4fe 8327c4fc .7......#4...'..
0x00010290 2322f4fe 833784fe 1387f7ff 2334e4fe #"...7......#4..
0x000102a0 23800700 6f008004 832784fc 032744fe #...o....'...'D.
0x000102b0 bb77f702 9b860700 833784fe 1387f7ff .w.......7......
0x000102c0 2334e4fe 13970602 13570702 930604ff #4.......W......
0x000102d0 3387e600 034707fe 2380e700 832784fc 3....G..#....'..
0x000102e0 032744fe bb57f702 2322f4fe 032784fc .'D..W..#"...'..
0x000102f0 832744fe 9b870700 e3f8e7fa 836744fe .'D..........gD.
0x00010300 130704ff b307f700 03c707fe 833784fe .............7..
0x00010310 2380e700 833784fe 13850700 03348103 #....7.......4..
0x00010320 13010104 67800000 130101fd 23348102 ....g.......#4..
0x00010330 13040103 233ca4fc 2338b4fc 833784fd ....#<..#8...7..
0x00010340 13871700 233ce4fc 83c70700 a307f4fe ....#<..........
0x00010350 833704fd 13871700 2338e4fc 83c70700 .7......#8......
0x00010360 2307f4fe 8347f4fe 93f7f70f 63900702 #....G......c...
0x00010370 8347f4fe 1b870700 8347e4fe 9b870700 .G.......G......
0x00010380 bb07f740 9b870700 6f000003 0347f4fe ...@....o....G..
0x00010390 8347e4fe 1377f70f 93f7f70f e300f7fa .G...w..........
0x000103a0 8347f4fe 1b870700 8347e4fe 9b870700 .G.......G......
0x000103b0 bb07f740 9b870700 13850700 03348102 ...@.........4..
0x000103c0 13010103 67800000 130101fc 233c1102 ....g.......#<..
0x000103d0 23388102 13040104 930784fc 13860700 #8..............
0x000103e0 9305a000 13050000 eff01fe5 13070500 ................
0x000103f0 b7070100 9385874d 13050700 eff0dff2 .......M........
0x00010400 93070500 13850700 eff01fdd 930784fc ................
0x00010410 13860700 9305a000 1305b007 eff0dfe1 ................
0x00010420 13070500 b7070100 9385074e 13050700 ...........N....
0x00010430 eff09fef 93070500 13850700 eff0dfd9 ................
0x00010440 930784fc 13860700 93050001 b7170000 ................
0x00010450 1385b72a eff05fde 13070500 b7070100 ...*.._.........
0x00010460 9385874e 13050700 eff01fec 93070500 ...N............
0x00010470 13850700 eff05fd6 930784fc 13860700 ......_.........
0x00010480 93050001 370500f0 eff01fdb 13070500 ....7...........
0x00010490 b7070100 9385074f 13050700 eff0dfe8 .......O........
0x000104a0 93070500 13850700 eff01fd3 13000000 ................
0x000104b0 83308103 03340103 13010104 67800000 .0...4......g...


Hex dump of section '.rodata':
0x000104c0 30313233 34353637 38394142 43444546 0123456789ABCDEF
0x000104d0 00000000 00000000 30000000 00000000 ........0.......
0x000104e0 31323300 00000000 31324142 00000000 123.....12AB....
0x000104f0 46303030 30303030 00000000 00000000 F0000000........
0x00010500 6e656761 74697665 00000000 00000000 negative........
0x00010510 7a65726f 00000000 706f7369 74697665 zero....positive
0x00010520 00 .


Hex dump of section '.data':
0x00011528 00050100 00000000 10050100 00000000 ................
0x00011538 18050100 00000000 ........

0 comments on commit 7040af1

Please sign in to comment.