//===========================================================================// // File: hash.tst // // Project: MUNGA Brick: Connection Library // // Contents: // //---------------------------------------------------------------------------// // Date Who Modification // // -------- --- ---------------------------------------------------------- // // 12/15/94 ECH Initial coding. // //---------------------------------------------------------------------------// // Copyright (C) 1994, Virtual World Entertainment, Inc. All Rights reserved // // PROPRIETARY AND CONFIDENTIAL // //===========================================================================// #include "time.hpp" class HashTestPlug: public Plug { public: int value; HashTestPlug(int value); ~HashTestPlug(); }; class HashTestNode: public Node { public: HashOf hash1; HashOf hash2; HashTestNode(); ~HashTestNode(); void RunProfile(); void RunTest(); }; #define RandInt(x) (rand() % x + 1) HashTestPlug::HashTestPlug(int value): Plug() { this->value = value; } HashTestPlug::~HashTestPlug() { } HashTestNode::HashTestNode(): Node(), hash1(TEST_CLASS/2, this, True), hash2(TEST_CLASS/2, this, True) { } HashTestNode::~HashTestNode() { } // //########################################################################### // ProfileClass //########################################################################### // Logical Hash::ProfileClass() { HashTestNode testNode; Time startTicks = Now(); Test_Message("Hash::ProfileClass \n"); testNode.RunProfile(); Test_Message("Hash::ProfileClass elapsed = " << (Now() - startTicks) << endl); return True; } // //########################################################################### // TestClass //########################################################################### // Logical Hash::TestClass() { DEBUG_STREAM << "Starting Hash test...\n"; HashTestNode testNode; testNode.RunTest(); return True; } void HashTestNode::RunProfile() { HashTestPlug *testPlug1, *testPlug2; int values[TEST_CLASS]; int i, j; Time startTicks; /* * Generate unique values, shuffle them */ for (i = 0; i < TEST_CLASS; i++) { values[i] = i; } for (i = 0; i < TEST_CLASS; i++) { int tmp; j = i + RandInt(TEST_CLASS - i) - 1; tmp = values[j]; values[j] = values[i]; values[i] = tmp; } // //-------------------------------------------------------------------- // Run timing tests //-------------------------------------------------------------------- // /* * Create plugs and add to both sockets */ startTicks = Now(); for (i = 0; i < TEST_CLASS; i++) { testPlug1 = new HashTestPlug(values[i]); Register_Object( testPlug1 ); hash1.AddValue(testPlug1, values[i]); hash2.AddValue(testPlug1, values[i]); } Test_Message("HashTestNode::RunTest Create = " << (Now() - startTicks) << endl); /* * Iterate over both sockets */ startTicks = Now(); { HashIteratorOf iterator1(hash1); HashIteratorOf iterator2(hash2); i = 0; while ((testPlug1 = iterator1.ReadAndNext()) != NULL) { Check( testPlug1 ); i++; } Verify( i == TEST_CLASS ); i = 0; while ((testPlug1 = iterator2.ReadAndNext()) != NULL) { Check( testPlug1 ); i++; } Verify( i == TEST_CLASS ); } Test_Message("HashTestNode::RunTest Iterate = " << (Now() - startTicks) << endl); /* * Find */ startTicks = Now(); { for (i = 0; i < TEST_CLASS; i++) { testPlug1 = hash1.Find(i); testPlug2 = hash2.Find(i); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); } } Test_Message("HashTestNode::RunTest Find = " << (Now() - startTicks) << endl); /* * Destroy from hash1, verify with hash2 */ startTicks = Now(); { HashIteratorOf iterator1(hash1); HashIteratorOf iterator2(hash2); i = 0; while ((testPlug1 = iterator1.ReadAndNext()) != NULL) { Check( testPlug1 ); i++; Unregister_Object(testPlug1); delete(testPlug1); } Verify( i == TEST_CLASS ); Verify( iterator2.GetCurrent() == NULL ); } Test_Message("HashTestNode::RunTest Destroy = " << (Now() - startTicks) << endl); } void HashTestNode::RunTest() { HashTestPlug *testPlug1, *testPlug2; int values[TEST_CLASS]; int i, j; // Time startTicks; /* * Generate unique values, shuffle them */ for (i = 0; i < TEST_CLASS; i++) { values[i] = i; } for (i = 0; i < TEST_CLASS; i++) { int tmp; j = i + RandInt(TEST_CLASS - i) - 1; tmp = values[j]; values[j] = values[i]; values[i] = tmp; } // //-------------------------------------------------------------------- // Stress tests //-------------------------------------------------------------------- // /* * Create plugs and add to both sockets */ for (i = 0; i < TEST_CLASS; i++) { testPlug1 = new HashTestPlug(values[i]); Register_Object( testPlug1 ); hash1.AddValue(testPlug1, values[i]); hash2.AddValue(testPlug1, values[i]); } /* * Find */ { for (i = 0; i < TEST_CLASS; i++) { testPlug1 = hash1.Find(i); testPlug2 = hash2.Find(i); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1->value == i ); Verify( testPlug2->value == i ); Verify( testPlug1 == testPlug2 ); } } /* * Test first and last */ { HashIteratorOf iterator1(hash1); HashIteratorOf iterator2(hash2); iterator1.First(); iterator2.First(); testPlug1 = iterator1.GetCurrent(); testPlug2 = iterator2.GetCurrent(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); } /* * Test next and prev */ { HashIteratorOf iterator1(hash1); HashIteratorOf iterator2(hash2); i = 0; while ((testPlug1 = iterator1.GetCurrent()) != NULL) { testPlug2 = iterator2.GetCurrent(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); iterator1.Next(); iterator2.Next(); i++; } Verify( i == TEST_CLASS ); } /* * Test read next and read prev */ { HashIteratorOf iterator1(hash1); HashIteratorOf iterator2(hash2); i = 0; while ((testPlug1 = iterator1.ReadAndNext()) != NULL) { testPlug2 = iterator2.ReadAndNext(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); i++; } Verify( i == TEST_CLASS ); } /* * Test Remove */ { HashIteratorOf iterator1(hash1); HashIteratorOf iterator2(hash2); i = 0; while ((testPlug1 = iterator1.GetCurrent()) != NULL) { testPlug2 = iterator2.GetCurrent(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); iterator1.Remove(); Unregister_Object(testPlug1); delete(testPlug1); i++; } Verify( iterator2.GetCurrent() == NULL ); Verify( i == TEST_CLASS ); } /* * Test random deletion */ /* * Add plugs to both sockets */ { HashIteratorOf iterator1(hash1); HashIteratorOf iterator2(hash2); Verify( iterator1.GetCurrent() == NULL ); Verify( iterator2.GetCurrent() == NULL ); for (i = 0; i < TEST_CLASS; i++) { testPlug1 = new HashTestPlug(values[i]); Register_Object( testPlug1 ); hash1.AddValue(testPlug1, values[i]); hash2.AddValue(testPlug1, values[i]); } } /* * Perform random deletion */ { int size, index; HashIteratorOf iterator1(hash1); HashIteratorOf iterator2(hash2); i = 0; iterator1.First(); while((size = iterator1.GetSize()) != 0) { index = RandInt(size) - 1; testPlug1 = iterator1.GetNth(index); Check( testPlug1 ); iterator1.Remove(); testPlug2 = iterator2.GetNth(index); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); Unregister_Object( testPlug2 ); delete(testPlug2); { HashIteratorOf iterator3(hash1); while ((testPlug1 = iterator3.ReadAndNext()) != NULL) { Check(testPlug1); } } i++; } Verify( i == TEST_CLASS ); Verify( iterator1.GetCurrent() == NULL ); Verify( iterator2.GetCurrent() == NULL ); } }