int main() { testInOrderPrint(); testPreOrderPrint(); testPostOrderPrint(); testSearch(); testIsEquiv(); testInsert(); printf("###########################\n"); }
int main(int argc, char ** argv) { string path = "/Users/andreasfragner/Desktop/"; // TEST STACK DIRECTION (misc.hpp) StackDirection(&argc); // SEARCH testSearch(); // SEEDS srand( int(time(NULL)) ); // TESTS/SPECS BitTest mytest; mytest.run(); // RANDOM testRandom(); // DATA STRUCTURES test_SL(); test_Stack(); test_Queue(); TowersOfHanoi(10); // NUMERICAL test_Numerical(); // PRIMES test_primes(); // SORT test_sort(); // MISC test_misc(); cout << "\n\n\n\n-----------------------------"<<endl; cout << "-----------------------------"<<endl; return 0; }
bool RFindInReadable_Impl( const StringT& aPattern, IteratorT& aSearchStart, IteratorT& aSearchEnd, const Comparator& compare ) { IteratorT patternStart, patternEnd, searchEnd = aSearchEnd; aPattern.BeginReading(patternStart); aPattern.EndReading(patternEnd); // Point to the last character in the pattern --patternEnd; // outer loop keeps searching till we run out of string to search while ( aSearchStart != searchEnd ) { // Point to the end position of the next possible match --searchEnd; // Check last character, if a match, explore further from here if ( compare(patternEnd.get(), searchEnd.get(), 1, 1) == 0 ) { // We're at a potential match, let's see if we really hit one IteratorT testPattern(patternEnd); IteratorT testSearch(searchEnd); // inner loop verifies the potential match at the current position do { // if we verified all the way to the end of the pattern, then we found it! if ( testPattern == patternStart ) { aSearchStart = testSearch; // point to start of match aSearchEnd = ++searchEnd; // point to end of match return true; } // if we got to end of the string we're searching before we hit the end of the // pattern, we'll never find what we're looking for if ( testSearch == aSearchStart ) { aSearchStart = aSearchEnd; return false; } // test previous character for a match --testPattern; --testSearch; } while ( compare(testPattern.get(), testSearch.get(), 1, 1) == 0 ); } } aSearchStart = aSearchEnd; return false; }
void test() { extern void testFlips(); extern void testLastFlipCountGenerator(); extern void testUtils(); extern void testSearch(); extern void testSolve(); extern void testHash(); printCompileType(); testFlips(); testUtils(); testLastFlipCountGenerator(); testSearch(); testSolve(); testHash(); }
int main() { testSearch(); return 0; }
bool FindInReadable_Impl( const StringT& aPattern, IteratorT& aSearchStart, IteratorT& aSearchEnd, const Comparator& compare ) { bool found_it = false; // only bother searching at all if we're given a non-empty range to search if ( aSearchStart != aSearchEnd ) { IteratorT aPatternStart, aPatternEnd; aPattern.BeginReading(aPatternStart); aPattern.EndReading(aPatternEnd); // outer loop keeps searching till we find it or run out of string to search while ( !found_it ) { // fast inner loop (that's what it's called, not what it is) looks for a potential match while ( aSearchStart != aSearchEnd && compare(aPatternStart.get(), aSearchStart.get(), 1, 1) ) ++aSearchStart; // if we broke out of the `fast' loop because we're out of string ... we're done: no match if ( aSearchStart == aSearchEnd ) break; // otherwise, we're at a potential match, let's see if we really hit one IteratorT testPattern(aPatternStart); IteratorT testSearch(aSearchStart); // slow inner loop verifies the potential match (found by the `fast' loop) at the current position for(;;) { // we already compared the first character in the outer loop, // so we'll advance before the next comparison ++testPattern; ++testSearch; // if we verified all the way to the end of the pattern, then we found it! if ( testPattern == aPatternEnd ) { found_it = true; aSearchEnd = testSearch; // return the exact found range through the parameters break; } // if we got to end of the string we're searching before we hit the end of the // pattern, we'll never find what we're looking for if ( testSearch == aSearchEnd ) { aSearchStart = aSearchEnd; break; } // else if we mismatched ... it's time to advance to the next search position // and get back into the `fast' loop if ( compare(testPattern.get(), testSearch.get(), 1, 1) ) { ++aSearchStart; break; } } } } return found_it; }
int main() { printf("Working on testcase 1: Validating isSorted() function\n"); //Testcase 1: test isSorted() function int arr[] = {0, 2, 4, 6, 8, 10}; assert(isSorted(arr, sizeof(arr)/sizeof(arr[0]), 1)); int arr_1[] = {0, 2, 4, 6, 7, 8, 7, 10}; assert(isSorted(arr_1, sizeof(arr_1)/sizeof(arr_1[0]), 1) == 0); printf("Working on testcase 1: Validating isSorted() function: Completed successfully\n"); //Testcase 2: test hasDuplicates() function printf("Working on testcase 2: Validating hasDuplicates() function\n"); int arr_2[] = {2, 5, 11, 14, 15, 18, 19, 20}; assert(hasDuplicates(arr_2, sizeof(arr_2)/sizeof(arr_2[0]), 1) == 0); assert(hasDuplicates(arr_1, sizeof(arr_1)/sizeof(arr_1[0]), 1) == -1); int arr_3[] = {21, 17, 13, 10, 9, 8 , 5, 5, 4, 3, 2, 0}; assert(hasDuplicates(arr_3, sizeof(arr_3)/sizeof(arr_3[0]), 0) == 1); printf("Working on testcase 2: Validating hasDuplicates() function: Completed successfully\n"); //Testcase 3: test isValid() function printf("Working on testcase 3: Validating isValid() function\n"); assert(isValid(5, 6)); assert(isValid(-1, 6) == 0); assert(isValid(0, 4)); assert(isValid(10, 6) == 0); printf("Working on testcase 3: Validating isValid() function: Completed successfully\n"); //Testcase 4: test Search() function with all different searchType and searchResult combinations printf("Working on testcase 4: Validating Search() function\n"); int arr_a[] = {2, 5, 11, 14, 15, 18, 19, 20, 22, 25, 27, 28, 100}; int keys_a[NumSearchTypeEntries][NumSearchResultEntries]; int indices_a[NumSearchTypeEntries][NumSearchResultEntries]; keys_a[LessThan][FoundLess] = 101; indices_a[LessThan][FoundLess] = 12; keys_a[LessThan][NotFound] = -1; indices_a[LessThan][NotFound] = -1; keys_a[LessThanEquals][FoundLess] = 99; indices_a[LessThanEquals][FoundLess] = 11; keys_a[LessThanEquals][FoundExact] = 100; indices_a[LessThanEquals][FoundExact] = 12; keys_a[LessThanEquals][NotFound] = 1; indices_a[LessThanEquals][NotFound] = -1; keys_a[Equals][FoundExact] = 25; indices_a[Equals][FoundExact] = 9; keys_a[Equals][NotFound] = 0; indices_a[Equals][NotFound] = -1; keys_a[GreaterThan][FoundGreater] = 19; indices_a[GreaterThan][FoundGreater] = 7; keys_a[GreaterThan][NotFound] = 100; indices_a[GreaterThan][NotFound] = -1; keys_a[GreaterThanEquals][FoundGreater] = 23; indices_a[GreaterThanEquals][FoundGreater] = 9; keys_a[GreaterThanEquals][FoundExact] = 100; indices_a[GreaterThanEquals][FoundExact] = 12; keys_a[GreaterThanEquals][NotFound] = 101; indices_a[GreaterThanEquals][NotFound] = -1; testSearch(arr_a, sizeof(arr_a)/sizeof(arr_a[0]), 1 /*ascending*/, keys_a, indices_a); printf("Working on testcase 4: Validating Search() function: Completed successfully\n"); printf("Working on testcase 5: Validating Search() function\n"); int arr_d[] = {33, 29, 28, 26, 21, 17, 13, 10, 9, 8 , 5, 4, 3, 2, 0}; int keys_d[NumSearchTypeEntries][NumSearchResultEntries]; int indices_d[NumSearchTypeEntries][NumSearchResultEntries]; keys_d[LessThan][FoundLess] = 34; indices_d[LessThan][FoundLess] = 0; keys_d[LessThan][NotFound] = -2; indices_d[LessThan][NotFound] = -1; keys_d[LessThanEquals][FoundLess] = 30; indices_d[LessThanEquals][FoundLess] = 1; keys_d[LessThanEquals][FoundExact] = 21; indices_d[LessThanEquals][FoundExact] = 4; keys_d[LessThanEquals][NotFound] = -3; indices_d[LessThanEquals][NotFound] = -1; keys_d[Equals][FoundExact] = 10; indices_d[Equals][FoundExact] = 7; keys_d[Equals][NotFound] = 1; indices_d[Equals][NotFound] = -1; keys_d[GreaterThan][FoundGreater] = 19; indices_d[GreaterThan][FoundGreater] = 4; keys_d[GreaterThan][NotFound] = 50; indices_d[GreaterThan][NotFound] = -1; keys_d[GreaterThanEquals][FoundGreater] = 23; indices_d[GreaterThanEquals][FoundGreater] = 3; keys_d[GreaterThanEquals][FoundExact] = 13; indices_d[GreaterThanEquals][FoundExact] = 6; keys_d[GreaterThanEquals][NotFound] = 34; indices_d[GreaterThanEquals][NotFound] = -1; testSearch(arr_d, sizeof(arr_d)/sizeof(arr_d[0]), 0 /*descending*/, keys_d, indices_d); printf("Working on testcase 5: Validating Search() function: Completed successfully\n"); return 0; }
void testSearch(struct DHTMessage** outMessagePtr, struct RouterModule* routerModule, struct DHTModuleRegistry* registry, struct Allocator* allocator) { *outMessagePtr = NULL; #define REQUEST_HASH "\xfc\x01\x01\x01\x01\x01\x01\x01\x21\x01\x01\x01\x01\x01\x01\x01" struct DHTMessage* callbackMessage = NULL; RouterModule_beginSearch((uint8_t*) REQUEST_HASH, testSearch_callback, &callbackMessage, routerModule); struct DHTMessage* outMessage = *outMessagePtr; assert(outMessage != NULL); // Need to be able to work around the fact that the string contains nulls. #define EXPECTED_OUTPUT(tid) \ "d" \ "1:q" "2:fn" \ "3:tar" "16:" REQUEST_HASH \ "4:txid" "2:" tid \ "e" for (uint32_t i = 0; i < (uint32_t) outMessage->length; i++) { //printf("%.2X", (unsigned int) outMessage->bytes[i] & 0xFF); } //printf("\n%s\n", outMessage->bytes); //printf("\n%s\n", outMessage->peerAddress); assert(outMessage->length == strlen(EXPECTED_OUTPUT("xx"))); assert(memcmp(outMessage->bytes, EXPECTED_OUTPUT("8\x00"), outMessage->length) == 0); //assert(strcmp(outMessage->address->networkAddress, " 00014 ") == 0); // In a normal DHT, 00014 is the closest node, however, 00011 has sent us a message in // testQuery() and thus his reach is 1 and he beats all other nodes which are 0-reach. // Search queries are allowed to select nodes which are further from the target than us. assert(strcmp((char*) &outMessage->address->networkAddress_be, " 00011 ") == 0); #undef EXPECTED_OUTPUT #define CRAFTED_REPLY(tid) \ "d" \ "1:n" "200:" \ "97bkjs8qpd5hc1mubj3qbfyqzmzxp3rg" " 00017 " \ "by1szn122nqk1vncjtm612444rlh6ztr" " 00018 " \ "u42wbyr0wznhkbqr1r7u627dwsvb8853" " 00019 " \ "97bkjs8qpd5hc1mubj3qbfyqzmzxp3rg" " 00020 " \ "2lr8w01hhrxqng8mm8nf3nlwh5nyxzyl" " 00021 " \ "4:txid" "2:" tid \ "e" struct Address address = { .key = "ponmlkjihgzyxwvutsrq \0" }; memcpy(&address.networkAddress_be, " 00011 ", 8); struct DHTMessage message = { .length = strlen(CRAFTED_REPLY("xx")), .allocator = allocator, .address = &address }; memcpy(message.bytes, CRAFTED_REPLY("8\x00"), message.length); // memcpy(message.peerAddress, peerAddress, 18); *outMessagePtr = NULL; DHTModules_handleIncoming(&message, registry); // Make sure the callback was called. assert(callbackMessage != NULL); // Make sure the node was promoted for it's fine service :P struct Address addr; memset(&addr, 0, sizeof(struct Address)); memcpy(&addr.key, "ponmlkjihgzyxwvutsrq \0", 32); struct Node* node1 = NodeStore_getNode(routerModule->nodeStore, &addr); //printf("node reach = %d", node1->reach); assert(node1->reach == 1601894175); /* outMessage = *outMessagePtr; assert(outMessage != NULL); assert(strcmp("000022", outMessage->peerAddress) == 0);*/ } int main() { char buffer[1<<20]; struct Allocator* allocator = BufferAllocator_new(buffer, 1<<20); struct DHTModuleRegistry* registry = DHTModules_new(allocator); ReplyModule_register(registry, allocator); struct RouterModule* routerModule = RouterModule_register(registry, allocator, (uint8_t*) MY_ADDRESS, event_base_new(), NULL); SerializationModule_register(registry, allocator); struct DHTMessage* outMessage; // dummy "network module" which just catches outgoing messages and makes them available. TestFramework_registerOutputCatcher(&outMessage, registry, allocator); struct Address addr; // damn this \0, was a mistake but to fix it would break all of the hashes :( #define ADD_NODE(address, netAddr) \ memset(&addr, 0, sizeof(struct Address)); \ memcpy(&addr.networkAddress_be, netAddr " ", 8); \ memcpy(&addr.key, (uint8_t*) address " \0", 32); \ RouterModule_addNode(&addr, routerModule) // most significant byte --vv ADD_NODE("qponmlkjihgzyxwvutsr", " 00001"); // fce8:573b:d230:ca3b 1c4e:f9d6 0632:9445 ADD_NODE("bcdefghijklmnopqrstu", " 00002"); // fc65:9f4c:c061:84f9 2018:6e31 de3d:3bcf ADD_NODE("onmlkjihgzyxwvutsrqp", " 00003"); // fcbe:26ce:5c7a:9a0f 205b:358c b8f8:08bb // search target --> fc01:0101:0101:0101 2101:0101 0101:0101 ADD_NODE("mlkjihgzyxwvutsrqpon", " 00004"); // fc08:d8e6:e000:c95c 2192:d676 94f9:63a7 ADD_NODE("lkjihgzyxwvutsrqponm", " 00005"); // fc7c:6c8d:e5ee:cf99 2f16:06c7 95ca:0c0b ADD_NODE("fghijklmnopqrstuvwxy", " 00006"); // fcac:3963:cbd1:6390 3e83:be89 a23f:ce66 ADD_NODE("jihgzyxwvutsrqponmlk", " 00007"); // fc2e:3a5e:5e47:9769 4964:8f7f 3894:8c07 ADD_NODE("kjihgzyxwvutsrqponml", " 00008"); // fc37:10aa:39ed:12bf 4a70:1507 dbe9:a054 ADD_NODE("cdefghijklmnopqrstuv", " 00009"); // fcaa:113e:88da:d432 65f0:1c14 38d9:b656 // this node --> fc39:c3ba:c711:00aa 666d:90b0 1ab6:e8c3 ADD_NODE("rqponmlkjihgzyxwvuts", " 00010"); // fc43:41e7:2adc:d13b 78ce:1959 e4cc:c76e ADD_NODE("ponmlkjihgzyxwvutsrq", " 00011"); // fc83:2ab3:b65b:9ad1 7e7b:f61f e0fa:cb40 ADD_NODE("efghijklmnopqrstuvwx", " 00012"); // fc08:0ba6:a8e2:7731 9dae:f9fd e502:767c ADD_NODE("abcdefghijklmnopqrst", " 00013"); // fc81:cab3:eda0:61aa 9fff:bdde 0168:f0dd ADD_NODE("ihgzyxwvutsrqponmlkj", " 00014"); // fc49:8fc7:7e43:981a bac1:0b5d 77fb:8818 ADD_NODE("nmlkjihgzyxwvutsrqpo", " 00015"); // fc69:61a1:2bec:1444 bb9e:47d1 f8b3:a6a0 ADD_NODE("defghijklmnopqrstuvw", " 00016"); // fc40:1d18:89a6:9a7e c8af:20fd 5c9f:8140 ADD_NODE("ghijklmnopqrstuvwxyz", " 00017"); // fc74:0f2e:d77a:e5e7 cf4e:8fe9 7791:98e1 #undef ADD_NODE testQuery(&outMessage, registry, allocator); testSearch(&outMessage, routerModule, registry, allocator); return 0; }