int hamming_search_test(const char* filename) { long result = errors_counter(); typedef containers::ternary_tree<std::string, const char*> Tst; Tst names; names["ABCD"] = "ABCD"; names["Abcd"] = "Abcd"; names["ABcd"] = "ABcd"; names["aBCd"] = "aBCd"; names["abc"] = "abc"; names["abcde"] = "abcde"; names["bcd"] = "bcd"; names["abCD"] = "abCD"; names["abCd"] = "abCd"; names["AbcD"] = "AbcD"; names["ABcD"] = "ABcD"; names["aBCD"] = "aBCD"; names["abCDE"] = "abCDE"; names["abCDd"] = "abCDd"; names["abCcd"] = "abCcd"; names["bcdc"] = "bcdc"; names["aab"] = "aab"; // hamming_search(2): ABcd, AbcD, Abcd, aBCd, abCD, abCd, abc, abcde // levenshtein_search(2): hamming + abCDd, abCcd bcd, bcdc Tst::search_results_list matches = names.create_search_results(); typedef Tst::search_results_list::iterator ResultIter; //names.levenshtein_search_count = 0; names.levenshtein_search("abcd", std::back_inserter(matches), 2); //std::cout << "l-count: " << names.levenshtein_search_count << "\n"; //names.levenshtein_search_count = 0; BOOST_CHECK(matches.size() == 12); if (matches.size() != 12) { // compare with DDJ { Tptr root = 0; for (Tst::iterator it = names.begin(); it != names.end(); ++it) { insertstr = (char*)*it; root = insert2(root, insertstr); } nearsearch(root, "abcd", 2); std::cout << "DDJ nearsearch abcd:\n"; for (int i = 0; i != srchtop; i++) std::cout << srcharr[i] << "\n"; std::cout << "\n"; cleanup2(root); } int i = 0; for (ResultIter rit = matches.begin(); rit != matches.end(); ++rit, ++i) { std::cout << **rit << " = " << (*rit).key(); std::cout /*<< " = " << matches[i].key()*/ << "\n"; } } typedef std::vector<std::string> Dictionary; extern size_t fill_dictionary(const char*, Dictionary&, size_t, size_t = 0); Dictionary dictionary; size_t longest_in_file = fill_dictionary(filename, dictionary, 300); std::random_shuffle(dictionary.begin(), dictionary.end()); names.clear(); Dictionary wildnames; // Add names with mini-variations long zeroes = 0; Dictionary::iterator dit; for (dit = dictionary.begin(); dit != dictionary.end(); ++dit) { const std::string& name(*dit); std::string namecopy(*dit); names[namecopy] = name.c_str(); std::string searchstr(name); for (int i = 0; i < 5; ++i) { int where = rand() % name.size(); // make string with ? at place of changed char if (searchstr[where] == '?') continue; searchstr[where] = '?'; wildnames.push_back(searchstr); char c = (char)rand(); if (!c) zeroes++; namecopy[where] = c; names[namecopy] = name.c_str(); } } for(dit = wildnames.begin(); dit != wildnames.end(); ++dit) { const std::string& name(*dit); for (int diff = 1; diff != 3; ++diff) { Tst::search_results_list matchresults = names.create_search_results(); names.hamming_search(name, std::back_inserter(matchresults), diff); //if (matchresults.size() == 0) // std::cout << "couldn't find " << name << '\n'; //BOOST_CHECK(found == matchresults.size()); for (size_t i = 0; i != matchresults.size(); ++i) BOOST_CHECK(check_hamming_match(matchresults[i].key(), name, diff)); } } return errors_counter() - result; }
int basic_insertion_test() { long result = errors_counter(); typedef containers::ternary_tree<std::basic_string<CharT>, int> Tst; Tst tst; const char* strings[] = { "\0", "aa", "aab", "aac", "add", "aee", "bab" }; widen<CharT> w; // Construction postconditions check_empty(tst); // Standard insert() tst.insert(std::make_pair(w(strings[1]), 1)); tst.insert(std::make_pair(w(strings[5]), 5)); std::pair<std::basic_string<CharT>, int> inval(w(strings[3]), 3); std::pair<Tst::iterator, bool> vr = tst.insert(inval); BOOST_CHECK(vr.first != tst.end()); BOOST_CHECK(vr.second == true); vr = tst.insert(inval); BOOST_CHECK(vr.first != tst.end()); BOOST_CHECK(vr.second == false); BOOST_CHECK((*tst.find(w(strings[1]))) == 1); BOOST_CHECK((*tst.find(w(strings[3]))) == 3); BOOST_CHECK((*tst.find(w(strings[5]))) == 5); BOOST_CHECK(tst.item_count() == 3); BOOST_CHECK(tst.total_key_length() == 3*3-1); // In present version, no reassignment tst.insert(std::make_pair(w(strings[1]), 4)); BOOST_CHECK((*tst.find(w(strings[1]))) == 1); BOOST_CHECK(tst.item_count() == 3); BOOST_CHECK(tst.total_key_length() == 3*3-1); // reference_proxy insert() tst[w(strings[2])] = 2; tst[w(strings[6])] = 6; tst[w(strings[4])] = 4; BOOST_CHECK(tst[w(strings[2])] == 2); BOOST_CHECK(tst[w(strings[4])] == 4); BOOST_CHECK(tst[w(strings[6])] == 6); BOOST_CHECK(tst.item_count() == 6); BOOST_CHECK(tst.total_key_length() == 6*3-1); // Now do reassignment tst[w(strings[1])] = 7; BOOST_CHECK((*tst.find(w(strings[1]))) == 7); //1); tst[w(strings[1])] = 1; // iterator used to check sort order of strings Tst::const_iterator it(tst.begin()); int expect_val = 1; while (it != tst.end()) { Tst::value_type val = *it; // string == "a", "b" etc BOOST_CHECK(expect_val == 1 || it.key().size() == 3); BOOST_CHECK(it.key() == w(strings[expect_val])); // value is 1, 2 etc BOOST_CHECK(val == expect_val++); ++it; } BOOST_CHECK(expect_val == int(tst.item_count() + 1)); // Test clear() postconditions tst.clear(); check_empty(tst); return errors_counter() - result; }