PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/test_utils.c

https://bitbucket.org/jasonlee28/proj2-agh
C | 200 lines | 145 code | 27 blank | 28 comment | 15 complexity | f82400f7cda27db7a30723490960a154 MD5 | raw file
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <CUnit/Basic.h>
  4. #include "utils.h"
  5. #include "types.h"
  6. #include "part2.c"
  7. void test_sign_extend_number();
  8. void test_parse_instruction_rtype();
  9. void test_parse_instruction_itype();
  10. void test_parse_instruction_stype();
  11. void test_parse_instruction_sbtype();
  12. void test_parse_instruction_ujtype();
  13. void test_parse_instruction_utype();
  14. void test_load();
  15. void test_store();
  16. int main(int arc, char **argv) {
  17. CU_pSuite pSuite1 = NULL;
  18. if (CUE_SUCCESS != CU_initialize_registry()) {
  19. return CU_get_error();
  20. }
  21. pSuite1 = CU_add_suite("Testing sign_extend_number", NULL, NULL);
  22. if (!pSuite1) {
  23. goto exit;
  24. }
  25. if (!CU_add_test(pSuite1, "test_sign_extend_number", test_sign_extend_number)) {
  26. goto exit;
  27. }
  28. if (!CU_add_test(pSuite1, "test_parse_instruction_rtype", test_parse_instruction_rtype)) {
  29. goto exit;
  30. }
  31. if (!CU_add_test(pSuite1, "test_parse_instruction_itype", test_parse_instruction_itype)) {
  32. goto exit;
  33. }
  34. if (!CU_add_test(pSuite1, "test_parse_instruction_stype", test_parse_instruction_stype)) {
  35. goto exit;
  36. }
  37. if (!CU_add_test(pSuite1, "test_parse_instruction_sbtype", test_parse_instruction_sbtype)) {
  38. goto exit;
  39. }
  40. if (!CU_add_test(pSuite1, "test_parse_instruction_ujtype", test_parse_instruction_ujtype)) {
  41. goto exit;
  42. }
  43. if (!CU_add_test(pSuite1, "test_parse_instruction_utype", test_parse_instruction_utype)) {
  44. goto exit;
  45. }
  46. if(!CU_add_test(pSuite1, "test_load", test_load)) {
  47. goto exit;
  48. }
  49. if(!CU_add_test(pSuite1, "test_store", test_store)) {
  50. goto exit;
  51. }
  52. CU_basic_set_mode(CU_BRM_VERBOSE);
  53. CU_basic_run_tests();
  54. exit:
  55. CU_cleanup_registry();
  56. return CU_get_error();
  57. }
  58. void test_sign_extend_number() {
  59. CU_ASSERT_EQUAL(sign_extend_number(0xFF, 8), 0xFFFFFFFF);
  60. CU_ASSERT_EQUAL(sign_extend_number(0xFF, 9), 0xFF);
  61. CU_ASSERT_EQUAL(sign_extend_number(0x1234, 16), 0x1234);
  62. CU_ASSERT_EQUAL(sign_extend_number(0x1234, 13), 0xFFFFF234);
  63. CU_ASSERT_EQUAL(sign_extend_number(0x0, 1), 0);
  64. CU_ASSERT_EQUAL(sign_extend_number(0x1, 1), 0xFFFFFFFF);
  65. }
  66. void test_parse_instruction_rtype() {
  67. Instruction inst;
  68. inst = parse_instruction(0x009402b3);
  69. CU_ASSERT_EQUAL(inst.rtype.opcode, 0x33);
  70. CU_ASSERT_EQUAL(inst.rtype.funct3, 0x0);
  71. CU_ASSERT_EQUAL(inst.rtype.rd, 0x5);
  72. CU_ASSERT_EQUAL(inst.rtype.rs1, 0x8);
  73. CU_ASSERT_EQUAL(inst.rtype.rs2, 0x9);
  74. CU_ASSERT_EQUAL(inst.rtype.funct7, 0x0);
  75. }
  76. void test_parse_instruction_itype() {
  77. Instruction inst;
  78. inst = parse_instruction(0x00a50313);
  79. CU_ASSERT_EQUAL(inst.itype.opcode, 0x13);
  80. CU_ASSERT_EQUAL(inst.itype.funct3, 0x0);
  81. CU_ASSERT_EQUAL(inst.itype.rd, 0x6);
  82. CU_ASSERT_EQUAL(inst.itype.rs1, 10);
  83. CU_ASSERT_EQUAL(inst.itype.imm, 10);
  84. }
  85. void test_parse_instruction_stype() {
  86. Instruction inst;
  87. inst = parse_instruction(0x014aa023);
  88. CU_ASSERT_EQUAL(inst.stype.opcode, 0x23);
  89. CU_ASSERT_EQUAL(inst.stype.funct3, 0x2);
  90. CU_ASSERT_EQUAL(inst.stype.rs1, 21);
  91. CU_ASSERT_EQUAL(inst.stype.rs2, 20);
  92. CU_ASSERT_EQUAL(inst.stype.imm7, 0);
  93. CU_ASSERT_EQUAL(inst.stype.imm5, 0);
  94. }
  95. void test_parse_instruction_sbtype() {
  96. Instruction inst;
  97. inst = parse_instruction(0x00058063);
  98. CU_ASSERT_EQUAL(inst.sbtype.opcode, 0x63);
  99. CU_ASSERT_EQUAL(inst.sbtype.funct3, 0x0);
  100. CU_ASSERT_EQUAL(inst.sbtype.rs1, 11);
  101. CU_ASSERT_EQUAL(inst.sbtype.rs2, 0);
  102. CU_ASSERT_EQUAL(inst.sbtype.imm7, 0);
  103. CU_ASSERT_EQUAL(inst.sbtype.imm5, 0);
  104. }
  105. void test_parse_instruction_utype() {
  106. Instruction inst;
  107. inst = parse_instruction(0xFFFFF437);
  108. CU_ASSERT_EQUAL(inst.utype.opcode, 0x37);
  109. CU_ASSERT_EQUAL(inst.utype.rd, 8);
  110. CU_ASSERT_EQUAL(inst.utype.imm, 0xFFFFF);
  111. }
  112. void test_parse_instruction_ujtype() {
  113. Instruction inst;
  114. inst = parse_instruction(0x000000EF);
  115. CU_ASSERT_EQUAL(inst.ujtype.opcode, 0x6F);
  116. CU_ASSERT_EQUAL(inst.ujtype.rd, 1);
  117. CU_ASSERT_EQUAL(inst.ujtype.imm, 0);
  118. }
  119. void test_load() {
  120. Byte mem[] = {0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
  121. /*
  122. * -----------------------------
  123. * | 0x15 | 0x14 | 0x13 | 0x12 |
  124. * Memory: -----------------------------
  125. * | 0x11 | 0x10 | 0x09 | 0x08 |
  126. * -----------------------------
  127. */
  128. CU_ASSERT_EQUAL(load(mem, 4, LENGTH_WORD), 0x15141312);
  129. CU_ASSERT_EQUAL(load(mem, 4, LENGTH_HALF_WORD), 0x1312);
  130. CU_ASSERT_EQUAL(load(mem, 4, LENGTH_BYTE), 0x12);
  131. /*printf("\n|0x%x|\n", load(mem, 4, LENGTH_WORD)); // output: 0x15141312
  132. printf("|0x%x|\n", load(mem, 4, LENGTH_HALF_WORD)); // output: 0x1312
  133. printf("|0x%x|\n", load(mem, 4, LENGTH_BYTE)); // output: 0x12
  134. */
  135. }
  136. void test_store() {
  137. Byte mem[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  138. int i = 0;
  139. int sizeofmem = 8;
  140. /* This is what we want the memory to look like!
  141. * -----------------------------
  142. * | 0x15 | 0x14 | 0x13 | 0x12 |
  143. * Memory: -----------------------------
  144. * | 0x11 | 0x10 | 0x09 | 0x08 |
  145. * -----------------------------
  146. */
  147. store(mem, 4, LENGTH_WORD, (Word) 0x15141312);
  148. Byte refmem1[] = {0x00, 0x00, 0x00, 0x00, 0x12, 0x13, 0x14, 0x15};
  149. //fprintf(stderr, "\n||FIRST||\n");
  150. for (; i < sizeofmem; i++) {
  151. //fprintf(stderr, "|0x%x|0x%x|\n", mem[i], refmem1[i]);
  152. CU_ASSERT_EQUAL(mem[i], refmem1[i]);
  153. }
  154. //fprintf(stderr, "\n||SECOND||\n");
  155. store(mem, 2, LENGTH_HALF_WORD, (Word) 0x1110);
  156. Byte refmem2[] = {0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
  157. for (i = 0; i < sizeofmem; i++) {
  158. //fprintf(stderr, "|0x%x|0x%x|\n", mem[i], refmem2[i]);
  159. CU_ASSERT_EQUAL(mem[i], refmem2[i]);
  160. }
  161. //fprintf(stderr, "\n||THIRD||\n");
  162. store(mem, 1, LENGTH_BYTE, (Word) 0x09);
  163. store(mem, 0, LENGTH_BYTE, (Word) 0x08);
  164. Byte refmem3[] = {0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
  165. for (i = 0; i < sizeofmem; i++) {
  166. //fprintf(stderr, "|0x%x|0x%x|\n", mem[i], refmem3[i]);
  167. CU_ASSERT_EQUAL(mem[i], refmem3[i]);
  168. }
  169. /*printf("\n|0x%x|\n", load(mem, 4, LENGTH_WORD)); // output: 0x15141312
  170. printf("|0x%x|\n", load(mem, 4, LENGTH_HALF_WORD)); // output: 0x1312
  171. printf("|0x%x|\n", load(mem, 4, LENGTH_BYTE)); // output: 0x12
  172. */
  173. }