ユニットテストで、コマンド表の内容をハッシュ表のまま出力するものと、ソートして出力するものに分離
authorj8takagi <j8takagi@nifty.com>
Tue, 23 Feb 2010 22:12:14 +0000 (07:12 +0900)
committerj8takagi <j8takagi@nifty.com>
Tue, 23 Feb 2010 22:23:16 +0000 (07:23 +0900)
12 files changed:
include/casl2.h
src/cmd.c
test/unit/print_cmdtype_code/0.txt
test/unit/print_cmdtype_code/print_cmdtype_code.c
test/unit/print_cmdtype_code_hash/0.txt [new file with mode: 0644]
test/unit/print_cmdtype_code_hash/Makefile [new file with mode: 0644]
test/unit/print_cmdtype_code_hash/print_cmdtype_code.c [new file with mode: 0644]
test/unit/print_code_type/0.txt
test/unit/print_code_type/print_code_type.c
test/unit/print_code_type_hash/0.txt [new file with mode: 0644]
test/unit/print_code_type_hash/Makefile [new file with mode: 0644]
test/unit/print_code_type_hash/print_code_type.c [new file with mode: 0644]

index 7cd937c..96363ea 100644 (file)
@@ -78,6 +78,8 @@ typedef enum {
     NONE = 0,
 } CMDTYPE;
 
+extern int cmdcodesize;
+
 /* 命令コードの配列 */
 typedef struct {
     char *cmd;
@@ -91,6 +93,9 @@ typedef struct _CMDCODETAB {
     CMDCODEARRAY *cca;
 } CMDCODETAB;
 
+extern CMDCODETAB *cmdtype_code[];
+extern CMDCODETAB *code_type[];
+
 /* 命令と命令タイプがキーのハッシュ表を作成する */
 bool create_cmdtype_code();
 
index aba0edd..df93682 100644 (file)
--- a/src/cmd.c
+++ b/src/cmd.c
@@ -97,21 +97,6 @@ WORD getcmdcode(const char *cmd, CMDTYPE type)
     return 0xFFFF;
 }
 
-/* 命令と命令タイプがキーのハッシュ表を表示する */
-void print_cmdtype_code()
-{
-    int i;
-    CMDCODETAB *np;
-    for(i = 0; i < cmdcodesize; i++){
-        np = cmdtype_code[i];
-        while(np != NULL) {
-            fprintf(stdout, "(%2d) - %s\t0%02o\t#%04X\n",
-                    i, np->cca->cmd, np->cca->type, np->cca->code);
-            np = np->next;
-        }
-    }
-}
-
 /* 命令と命令タイプがキーのハッシュ表を解放する */
 void free_cmdtype_code()
 {
@@ -175,19 +160,6 @@ CMDTYPE getcmdtype(WORD code)
     return NONE;
 }
 
-/* 命令コードがキーのハッシュ表を表示する */
-void print_code_type()
-{
-    int i;
-    CMDCODETAB *np;
-    for(i = 0; i < cmdcodesize; i++){
-        for(np = code_type[i]; np != NULL; np = np->next) {
-            fprintf(stdout, "(%2d) - #%04X\t0%02o\t%s\n",
-                    i, np->cca->code, np->cca->type, np->cca->cmd);
-        }
-    }
-}
-
 /* 命令コードがキーのハッシュ表を解放する */
 void free_code_type()
 {
index 4c2d87a..34a0a04 100644 (file)
@@ -1,39 +1,38 @@
-== CMDTYPE_CODE TABLE ==
-( 0) - SVC     030     #F000
-( 1) - JZE     030     #6300
-( 1) - SLL     010     #5200
-( 2) - SLA     010     #5000
-( 3) - ADDL    011     #2200
-( 3) - NOP     000     #0000
-( 4) - ADDA    011     #2000
-( 7) - OR      020     #3500
-(10) - CALL    030     #8000
-(11) - ADDL    020     #2600
-(12) - ADDA    020     #2400
-(13) - RET     000     #8100
-(14) - SUBL    011     #2300
-(15) - SUBA    011     #2100
-(19) - LAD     010     #1200
-(20) - JMI     030     #6100
-(21) - AND     011     #3000
-(22) - JUMP    030     #6400
-(22) - SUBL    020     #2700
-(23) - CPL     011     #4100
-(23) - SUBA    020     #2500
-(24) - CPA     011     #4000
-(25) - XOR     011     #3200
-(26) - JNZ     030     #6200
-(26) - LD      011     #1000
-(27) - JOV     030     #6600
-(29) - SRL     010     #5300
-(29) - AND     020     #3400
-(29) - ST      010     #1100
-(30) - SRA     010     #5100
-(31) - CPL     020     #4500
-(32) - JPL     030     #6500
-(32) - CPA     020     #4400
-(33) - POP     040     #7100
-(33) - XOR     020     #3600
-(34) - PUSH    030     #7000
-(34) - LD      020     #1400
-(37) - OR      011     #3100
+NOP    000     #0000
+LD     011     #1000
+ST     010     #1100
+LAD    010     #1200
+LD     020     #1400
+ADDA   011     #2000
+SUBA   011     #2100
+ADDL   011     #2200
+SUBL   011     #2300
+ADDA   020     #2400
+SUBA   020     #2500
+ADDL   020     #2600
+SUBL   020     #2700
+AND    011     #3000
+OR     011     #3100
+XOR    011     #3200
+AND    020     #3400
+OR     020     #3500
+XOR    020     #3600
+CPA    011     #4000
+CPL    011     #4100
+CPA    020     #4400
+CPL    020     #4500
+SLA    010     #5000
+SRA    010     #5100
+SLL    010     #5200
+SRL    010     #5300
+JMI    030     #6100
+JNZ    030     #6200
+JZE    030     #6300
+JUMP   030     #6400
+JPL    030     #6500
+JOV    030     #6600
+PUSH   030     #7000
+POP    040     #7100
+CALL   030     #8000
+RET    000     #8100
+SVC    030     #F000
index 9410051..a2dcf86 100644 (file)
@@ -1,6 +1,41 @@
 #include "casl2.h"
-int main(){
-    puts("== CMDTYPE_CODE TABLE ==");
+#include "cerr.h"
+#include <string.h>
+
+int compare_code(const void *a, const void *b)
+{
+    const CMDCODEARRAY ca = **(const CMDCODEARRAY **)a;
+    const CMDCODEARRAY cb = **(const CMDCODEARRAY **)b;
+    int diff;
+    if((diff = strcmp(ca.cmd, cb.cmd)) == 0) {
+        return ca.type - cb.type;
+    } else {
+        return diff;
+    }
+}
+
+/* 命令と命令タイプがキーのハッシュ表を表示する */
+void print_cmdtype_code()
+{
+    int i, j = 0;
+    CMDCODETAB *np;
+    CMDCODEARRAY **ar;
+    ar = malloc(sizeof(*ar) * cmdcodesize);
+    for(i = 0; i < cmdcodesize; i++) {
+        np = cmdtype_code[i];
+        while(np != NULL) {
+            ar[j++] = np->cca;
+            np = np->next;
+        }
+    }
+    qsort(ar, cmdcodesize, sizeof(*ar), (int (*)(const void*, const void*))compare_code);
+    for(i = 0; i < cmdcodesize; i++) {
+        fprintf(stdout, "%s\t0%02o\t#%04X\n", ar[i]->cmd, ar[i]->type, ar[i]->code);
+    }
+}
+
+int main()
+{
     create_cmdtype_code();
     print_cmdtype_code();
     free_cmdtype_code();
diff --git a/test/unit/print_cmdtype_code_hash/0.txt b/test/unit/print_cmdtype_code_hash/0.txt
new file mode 100644 (file)
index 0000000..a040405
--- /dev/null
@@ -0,0 +1,38 @@
+( 0) - SVC     030     #F000
+( 1) - JZE     030     #6300
+( 1) - SLL     010     #5200
+( 2) - SLA     010     #5000
+( 3) - ADDL    011     #2200
+( 3) - NOP     000     #0000
+( 4) - ADDA    011     #2000
+( 7) - OR      020     #3500
+(10) - CALL    030     #8000
+(11) - ADDL    020     #2600
+(12) - ADDA    020     #2400
+(13) - RET     000     #8100
+(14) - SUBL    011     #2300
+(15) - SUBA    011     #2100
+(19) - LAD     010     #1200
+(20) - JMI     030     #6100
+(21) - AND     011     #3000
+(22) - JUMP    030     #6400
+(22) - SUBL    020     #2700
+(23) - CPL     011     #4100
+(23) - SUBA    020     #2500
+(24) - CPA     011     #4000
+(25) - XOR     011     #3200
+(26) - JNZ     030     #6200
+(26) - LD      011     #1000
+(27) - JOV     030     #6600
+(29) - SRL     010     #5300
+(29) - AND     020     #3400
+(29) - ST      010     #1100
+(30) - SRA     010     #5100
+(31) - CPL     020     #4500
+(32) - JPL     030     #6500
+(32) - CPA     020     #4400
+(33) - POP     040     #7100
+(33) - XOR     020     #3600
+(34) - PUSH    030     #7000
+(34) - LD      020     #1400
+(37) - OR      011     #3100
diff --git a/test/unit/print_cmdtype_code_hash/Makefile b/test/unit/print_cmdtype_code_hash/Makefile
new file mode 100644 (file)
index 0000000..2b1571f
--- /dev/null
@@ -0,0 +1,3 @@
+UCLASS = COMMON
+TESTSRCFILE = ../CERRARRAY.c print_cmdtype_code.c
+include ../TEST.mk
diff --git a/test/unit/print_cmdtype_code_hash/print_cmdtype_code.c b/test/unit/print_cmdtype_code_hash/print_cmdtype_code.c
new file mode 100644 (file)
index 0000000..b10db02
--- /dev/null
@@ -0,0 +1,28 @@
+#include "casl2.h"
+
+/* 命令と命令タイプがキーのハッシュ表を表示する */
+void print_cmdtype_code()
+{
+    int i;
+    CMDCODETAB *np;
+    for(i = 0; i < cmdcodesize; i++){
+        np = cmdtype_code[i];
+        while(np != NULL) {
+            fprintf(stdout, "(%2d) - %s\t0%02o\t#%04X\n",
+                    i, np->cca->cmd, np->cca->type, np->cca->code);
+            np = np->next;
+        }
+    }
+}
+
+int main(){
+    create_cmdtype_code();
+    print_cmdtype_code();
+    free_cmdtype_code();
+    if(cerrno != 0) {
+        printf("\terror - %d: %s\n", cerrno, cerrmsg);
+        freecerr();
+        exit(-1);
+    }
+    return 0;
+}
index 67b1eb0..e34b3c6 100644 (file)
@@ -1,39 +1,38 @@
-== CODE_TYPE TABLE ==
-( 0) - #2600   020     ADDL
-( 0) - #0000   000     NOP
-( 1) - #2700   020     SUBL
-( 4) - #5000   010     SLA
-( 5) - #5100   010     SRA
-( 6) - #5200   010     SLL
-( 7) - #5300   010     SRL
-(10) - #3000   011     AND
-(11) - #3100   011     OR
-(12) - #F000   030     SVC
-(12) - #3200   011     XOR
-(14) - #8000   030     CALL
-(14) - #3400   020     AND
-(15) - #8100   000     RET
-(15) - #3500   020     OR
-(16) - #3600   020     XOR
-(16) - #1000   011     LD
-(17) - #1100   010     ST
-(18) - #1200   010     LAD
-(20) - #1400   020     LD
-(21) - #6100   030     JMI
-(22) - #6200   030     JNZ
-(23) - #6300   030     JZE
-(24) - #6400   030     JUMP
-(25) - #6500   030     JPL
-(26) - #6600   030     JOV
-(26) - #4000   011     CPA
-(27) - #4100   011     CPL
-(30) - #4400   020     CPA
-(31) - #4500   020     CPL
-(32) - #2000   011     ADDA
-(33) - #2100   011     SUBA
-(34) - #2200   011     ADDL
-(35) - #2300   011     SUBL
-(36) - #7000   030     PUSH
-(36) - #2400   020     ADDA
-(37) - #7100   040     POP
-(37) - #2500   020     SUBA
+#0000  000     NOP
+#1000  011     LD
+#1100  010     ST
+#1200  010     LAD
+#1400  020     LD
+#2000  011     ADDA
+#2100  011     SUBA
+#2200  011     ADDL
+#2300  011     SUBL
+#2400  020     ADDA
+#2500  020     SUBA
+#2600  020     ADDL
+#2700  020     SUBL
+#3000  011     AND
+#3100  011     OR
+#3200  011     XOR
+#3400  020     AND
+#3500  020     OR
+#3600  020     XOR
+#4000  011     CPA
+#4100  011     CPL
+#4400  020     CPA
+#4500  020     CPL
+#5000  010     SLA
+#5100  010     SRA
+#5200  010     SLL
+#5300  010     SRL
+#6100  030     JMI
+#6200  030     JNZ
+#6300  030     JZE
+#6400  030     JUMP
+#6500  030     JPL
+#6600  030     JOV
+#7000  030     PUSH
+#7100  040     POP
+#8000  030     CALL
+#8100  000     RET
+#F000  030     SVC
index 9635beb..51abfd3 100644 (file)
@@ -1,9 +1,33 @@
 #include "casl2.h"
 #include "cerr.h"
 
+int compare_code(const void *a, const void *b)
+{
+    return (**(const CMDCODEARRAY **)a).code - (**(const CMDCODEARRAY **)b).code;
+}
+
+/* 命令コードがキーのハッシュ表を表示する */
+void print_code_type()
+{
+    int i, j = 0;
+    CMDCODETAB *np;
+    CMDCODEARRAY **ar;
+    ar = malloc(sizeof(*ar) * cmdcodesize);
+    for(i = 0; i < cmdcodesize; i++) {
+        np = code_type[i];
+        while(np != NULL) {
+            ar[j++] = np->cca;
+            np = np->next;
+        }
+    }
+    qsort(ar, cmdcodesize, sizeof(*ar), (int (*)(const void*, const void*))compare_code);
+    for(i = 0; i < cmdcodesize; i++) {
+        fprintf(stdout, "#%04X\t0%02o\t%s\n", ar[i]->code, ar[i]->type, ar[i]->cmd);
+    }
+}
+
 int main()
 {
-    puts("== CODE_TYPE TABLE ==");
     create_code_type();
     print_code_type();
     free_code_type();
diff --git a/test/unit/print_code_type_hash/0.txt b/test/unit/print_code_type_hash/0.txt
new file mode 100644 (file)
index 0000000..206a466
--- /dev/null
@@ -0,0 +1,38 @@
+( 0) - #2600   020     ADDL
+( 0) - #0000   000     NOP
+( 1) - #2700   020     SUBL
+( 4) - #5000   010     SLA
+( 5) - #5100   010     SRA
+( 6) - #5200   010     SLL
+( 7) - #5300   010     SRL
+(10) - #3000   011     AND
+(11) - #3100   011     OR
+(12) - #F000   030     SVC
+(12) - #3200   011     XOR
+(14) - #8000   030     CALL
+(14) - #3400   020     AND
+(15) - #8100   000     RET
+(15) - #3500   020     OR
+(16) - #3600   020     XOR
+(16) - #1000   011     LD
+(17) - #1100   010     ST
+(18) - #1200   010     LAD
+(20) - #1400   020     LD
+(21) - #6100   030     JMI
+(22) - #6200   030     JNZ
+(23) - #6300   030     JZE
+(24) - #6400   030     JUMP
+(25) - #6500   030     JPL
+(26) - #6600   030     JOV
+(26) - #4000   011     CPA
+(27) - #4100   011     CPL
+(30) - #4400   020     CPA
+(31) - #4500   020     CPL
+(32) - #2000   011     ADDA
+(33) - #2100   011     SUBA
+(34) - #2200   011     ADDL
+(35) - #2300   011     SUBL
+(36) - #7000   030     PUSH
+(36) - #2400   020     ADDA
+(37) - #7100   040     POP
+(37) - #2500   020     SUBA
diff --git a/test/unit/print_code_type_hash/Makefile b/test/unit/print_code_type_hash/Makefile
new file mode 100644 (file)
index 0000000..17eb22a
--- /dev/null
@@ -0,0 +1,3 @@
+UCLASS = COMMON
+TESTSRCFILE = ../CERRARRAY.c print_code_type.c
+include ../TEST.mk
diff --git a/test/unit/print_code_type_hash/print_code_type.c b/test/unit/print_code_type_hash/print_code_type.c
new file mode 100644 (file)
index 0000000..c081888
--- /dev/null
@@ -0,0 +1,27 @@
+#include "casl2.h"
+#include "cerr.h"
+
+/* 命令コードがキーのハッシュ表を表示する */
+void print_code_type()
+{
+    int i;
+    CMDCODETAB *np;
+    for(i = 0; i < cmdcodesize; i++){
+        for(np = code_type[i]; np != NULL; np = np->next) {
+            fprintf(stdout, "(%2d) - #%04X\t0%02o\t%s\n", i, np->cca->code, np->cca->type, np->cca->cmd);
+        }
+    }
+}
+
+int main()
+{
+    create_code_type();
+    print_code_type();
+    free_code_type();
+    if(cerrno != 0) {
+        printf("\terror - %d: %s\n", cerrno, cerrmsg);
+        freecerr();
+        exit(-1);
+    }
+    return 0;
+}