PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/klein_C_code/src/smallVM/misc/tests.cpp

http://github.com/AdamSpitz/klein
C++ | 180 lines | 134 code | 34 blank | 12 comment | 26 complexity | d805f6578a65782ce56ceac3fff419be MD5 | raw file
  1. # include "wordLayout.hh"
  2. # include "bytesPartLayout.hh"
  3. # include "markLayout.hh"
  4. # include "byteVectorLayout.hh"
  5. # include "objVectorLayout.hh"
  6. # include "tag.hh"
  7. # include "slotType.hh"
  8. # include "slotTypeFields.incl.hh"
  9. # include "theVM.hh"
  10. # include "universe.hh"
  11. # include "headerFields.hh"
  12. # include "stdio.h"
  13. // A lot of these tests are very brittle and will break if we make any changes to
  14. // the layouts of objects. When that happens, we can either make these tests less
  15. // brittle, or else just throw them away. I mostly just want them for the early
  16. // stages of the project, to give me confidence that this code I'm writing actually
  17. // works. Once we have better end-to-end tests, these tests here will become less
  18. // useful (though maybe still useful enough to be worth debrittlifying them).
  19. // -- Adam, 11/05
  20. void always_assert_equal(int a, int b, char* aMsg, char* bMsg) {
  21. if ( a != b ) {
  22. printf("%i != %i\n", a, b );
  23. printf("%s != %s\n", aMsg, bMsg);
  24. error("assertion failure!");
  25. }
  26. }
  27. # define ASSERT_EQUAL(a, b) always_assert_equal(a, b, #a, #b)
  28. # define ASSERT_FAIL(x) { try {x ASSERT(false); } catch (...) {} }
  29. void testTag() {
  30. ASSERT( Tag::tagOfOop( (Oop) 0 ) == 0 );
  31. ASSERT( Tag::tagOfOop( (Oop) 1 ) == 1 );
  32. ASSERT( Tag::tagOfOop( (Oop) 2 ) == 2 );
  33. ASSERT( Tag::tagOfOop( (Oop) 3 ) == 3 );
  34. ASSERT( Tag::tagOfOop( (Oop) 16 ) == 0 );
  35. ASSERT( Tag::tagOfOop( (Oop) 57 ) == 1 );
  36. ASSERT( Tag::tagOfOop( (Oop) 94 ) == 2 );
  37. ASSERT( Tag::tagOfOop( (Oop) 7 ) == 3 );
  38. }
  39. void testSlotType() {
  40. ASSERT( isObjectSlot ( slotType_slotTypeField_objectSlotValue << slotType_slotTypeField_shift ) );
  41. ASSERT( isMapSlot ( slotType_slotTypeField_mapSlotValue << slotType_slotTypeField_shift ) );
  42. ASSERT( isArgumentSlot( slotType_slotTypeField_argumentSlotValue << slotType_slotTypeField_shift ) );
  43. ASSERT( isParent( 1 << slotType_isParentField_shift ) );
  44. ASSERT( !isParent( 0 << slotType_isParentField_shift ) );
  45. ASSERT( isAssignable(1 << slotType_isAssignableField_shift ) );
  46. ASSERT( !isAssignable(0 << slotType_isAssignableField_shift ) );
  47. SlotType assignableObjectSlotType = (1 << slotType_isAssignableField_shift) | (slotType_slotTypeField_objectSlotValue << slotType_slotTypeField_shift );
  48. ASSERT( isAssignable(assignableObjectSlotType) );
  49. ASSERT( isObjectSlot(assignableObjectSlotType) );
  50. }
  51. void testBytesPart() {
  52. BytesPartLayout bpLayout;
  53. BPRef bpRef = bpLayout.allocateBytesPartWithIndexableSize(4);
  54. ASSERT(bpLayout.indexableSizeOfBytesPart(bpRef) == 4);
  55. bpLayout.forBytesPart_At_Put( bpRef, 0, 'A');
  56. bpLayout.forBytesPart_At_Put( bpRef, 1, 'd');
  57. bpLayout.forBytesPart_At_Put( bpRef, 2, 'a');
  58. bpLayout.forBytesPart_At_Put( bpRef, 3, 'm');
  59. ASSERT (bpLayout.forBytesPart_At ( bpRef, 0) == 'A');
  60. ASSERT (bpLayout.forBytesPart_At ( bpRef, 1) == 'd');
  61. ASSERT (bpLayout.forBytesPart_At ( bpRef, 2) == 'a');
  62. ASSERT (bpLayout.forBytesPart_At ( bpRef, 3) == 'm');
  63. ASSERT_FAIL(bpLayout.forBytesPart_At ( bpRef, -1); );
  64. ASSERT_FAIL(bpLayout.forBytesPart_At ( bpRef, 4); );
  65. }
  66. void testMarkLayout() {
  67. MarkLayout markLayout;
  68. ASSERT(markLayout.encode(5) == 23);
  69. ASSERT(markLayout.decode(99) == 24);
  70. ASSERT(markLayout.valueOf(79) == 19);
  71. ASSERT(markLayout.trailingMark() == 3);
  72. ASSERT(markLayout.hashOfMarkValue(markLayout.set_hashOfMarkValue(17, 56)) == 56);
  73. ASSERT(markLayout.isMarkValueForByteVector(1));
  74. }
  75. void testMemoryObjectLayout() {
  76. MemoryObjectLayout moLayout;
  77. ASSERT(moLayout.memForAddress((Address) 4) == 5);
  78. ASSERT(moLayout.addressOfMem(9) == (Address) 8);
  79. Oop o = moLayout.memForAddress(theVM()->universe()->allocateOops(9));
  80. moLayout.setMarkValueOf(o, 7 );
  81. ASSERT_EQUAL( moLayout. markValueOf(o), 7 );
  82. ASSERT_EQUAL( moLayout.for_At(o, 0) , 31 );
  83. moLayout.setOIDOf(o, 13 );
  84. ASSERT_EQUAL( moLayout. oidOf(o), 13 );
  85. ASSERT_EQUAL( moLayout.for_At(o, 1), 52 );
  86. }
  87. void testObjVectorLayout() {
  88. ObjVectorLayout ovLayout;
  89. Oop o = ovLayout.memForAddress(theVM()->universe()->allocateOops(15));
  90. ovLayout.setMarkValueOf( o, ovLayout.indexableSizeField ()->markField()->wordMeaningUnencodable()
  91. | ovLayout.indexableOriginField()->markField()->wordMeaningUnencodable() );
  92. ovLayout.set_indexableSizeOf(o, 10);
  93. ASSERT( ovLayout. indexableSizeOf(o) == 10);
  94. ovLayout.set_indexableOriginOf(o, 5);
  95. ASSERT( ovLayout. indexableOriginOf(o) == 5);
  96. ASSERT_EQUAL((int) ovLayout.for_AddressOfIndexableAt(o, 0),
  97. (int) (ovLayout.addressOfMem(o) + 5));
  98. ovLayout.for_IndexableAt_Put( o, 0, 101);
  99. ovLayout.for_IndexableAt_Put( o, 1, 201);
  100. ovLayout.for_IndexableAt_Put( o, 2, 301);
  101. ovLayout.for_IndexableAt_Put( o, 9, 1001);
  102. ASSERT_EQUAL( ovLayout.for_IndexableAt ( o, 0), 101);
  103. ASSERT_EQUAL( ovLayout.for_IndexableAt ( o, 1), 201);
  104. ASSERT_EQUAL( ovLayout.for_IndexableAt ( o, 2), 301);
  105. ASSERT_EQUAL( ovLayout.for_IndexableAt ( o, 9), 1001);
  106. ASSERT_FAIL ( ovLayout.for_IndexableAt ( o, -1); );
  107. ASSERT_FAIL ( ovLayout.for_IndexableAt ( o, 10); );
  108. }
  109. void testByteVectorLayout() {
  110. ByteVectorLayout bvLayout;
  111. BytesPartLayout bpLayout;
  112. BPRef bpRef = bpLayout.allocateBytesPartWithIndexableSize(10);
  113. Oop o = bvLayout.memForAddress(theVM()->universe()->allocateOops(4));
  114. bvLayout.setMarkValueOf(o, 7);
  115. ASSERT(bvLayout. markValueOf(o) == 7);
  116. bvLayout.set_bytesPartRefOf(o, bpRef);
  117. ASSERT(bvLayout. bytesPartRefOf(o) == bpRef);
  118. bvLayout.for_IndexableAt_Put( o, 0, 'b' );
  119. bvLayout.for_IndexableAt_Put( o, 1, 'l' );
  120. bvLayout.for_IndexableAt_Put( o, 2, 'a' );
  121. bvLayout.for_IndexableAt_Put( o, 9, 'h' );
  122. ASSERT_EQUAL( bvLayout.for_IndexableAt ( o, 0), 'b' );
  123. ASSERT_EQUAL( bvLayout.for_IndexableAt ( o, 1), 'l' );
  124. ASSERT_EQUAL( bvLayout.for_IndexableAt ( o, 2), 'a' );
  125. ASSERT_EQUAL( bvLayout.for_IndexableAt ( o, 9), 'h' );
  126. ASSERT_FAIL ( bvLayout.for_IndexableAt ( o, 10); );
  127. }
  128. int argCountOfCString(char* s) {
  129. return argCountOfString(s, lengthOfCString(s));
  130. }
  131. void testArgCount() {
  132. ASSERT_EQUAL( argCountOfCString( "x" ), 0 );
  133. ASSERT_EQUAL( argCountOfCString( "x:" ), 1 );
  134. ASSERT_EQUAL( argCountOfCString( "blah:" ), 1 );
  135. ASSERT_EQUAL( argCountOfCString( "isAardvark" ), 0 );
  136. ASSERT_EQUAL( argCountOfCString( "to:By:Do:" ), 3 );
  137. }
  138. void runAllTests() {
  139. testTag();
  140. testSlotType();
  141. // testBytesPart();
  142. // testMarkLayout();
  143. // testMemoryObjectLayout();
  144. // testObjVectorLayout();
  145. // testByteVectorLayout();
  146. }