📄 hashtablediagnose.h
字号:
} // get all the keys (SYSTEM mode) // Vector<String> keys_list; char_ht_0.keys(keys_list); long index; if (keys_list.length() != num_elem) { return Error::handle(name(), L"keys", Error::TEST, __FILE__, __LINE__); } // check if we have got all the keys // for (long i = 0; i < num_elem; i++) { if (!keys_list.contains(index, &keys[i])) { return Error::handle(name(), L"keys", Error::TEST, __FILE__, __LINE__); } } // get all the keys (USER mode) // char_ht_2.keys(keys_list); if (keys_list.length() != num_elem) { return Error::handle(name(), L"keys", Error::TEST, __FILE__, __LINE__); } // check if we have got all the keys // for (long i = 0; i < num_elem; i++) { if (!keys_list.contains(index, &keys[i])) { return Error::handle(name(), L"keys", Error::TEST, __FILE__, __LINE__); } } // get all the values (SYSTEM mode) // Vector<Char> chars_list; char_ht_0.values(chars_list); if (chars_list.length() != num_elem) { return Error::handle(name(), L"values", Error::TEST, __FILE__, __LINE__); } // check if we have get all the values // for (long i = 0; i < num_elem; i++) { if (!chars_list.contains(index, items[i])) { return Error::handle(name(), L"values", Error::TEST, __FILE__, __LINE__); } } // get all the values (USER mode) // char_ht_2.values(chars_list); if (chars_list.length() != num_elem) { return Error::handle(name(), L"values", Error::TEST, __FILE__, __LINE__); } // check if we have get all the values // for (long i = 0; i < num_elem; i++) { if (!chars_list.contains(index, items[i])) { return Error::handle(name(), L"values", Error::TEST, __FILE__, __LINE__); } } // test contains (SYSTEM) // for (long i = 0; i < num_elem; i++) { if (!char_ht_0.containsKey(keys[i])) { keys[i].debug(L"this key is not found"); return Error::handle(name(), L"containsKey", Error::TEST, __FILE__, __LINE__); } if (!char_ht_0.containsValue(items[i])) { items[i]->debug(L"this value is not found"); return Error::handle(name(), L"containsValue", Error::TEST, __FILE__, __LINE__); } } // test contains (USER) // for (long i = 0; i < num_elem; i++) { if (!char_ht_2.containsKey(keys[i])) { keys[i].debug(L"this key is not found"); return Error::handle(name(), L"containsKey", Error::TEST, __FILE__, __LINE__); } if (!char_ht_2.containsValue(items[i])) { items[i]->debug(L"this value is not found"); return Error::handle(name(), L"containsValue", Error::TEST, __FILE__, __LINE__); } } // test remove (SYSTEM) // Char* char_ptr = new Char(); char_ht_0.remove(keys[0], char_ptr); if (char_ptr->ne(*items[0])) { return Error::handle(name(), L"remove", Error::TEST, __FILE__, __LINE__); } if (char_ht_0.containsKey(keys[0])) { return Error::handle(name(), L"remove / containsKey", Error::TEST, __FILE__, __LINE__); } if (char_ht_0.containsValue(items[0])) { return Error::handle(name(), L"remove / containsValue", Error::TEST, __FILE__, __LINE__); } delete char_ptr; char_ht_0.remove(keys[1]); if (char_ht_0.containsKey(keys[1])) { return Error::handle(name(), L"remove / containsKey", Error::TEST, __FILE__, __LINE__); } if (char_ht_0.containsValue(items[1])) { return Error::handle(name(), L"remove / containsValue", Error::TEST, __FILE__, __LINE__); } // test remove (USER) // char_ptr = (Char*)NULL; char_ht_2.remove(keys[0], char_ptr); if ((char_ptr == (Char*)NULL) || (char_ptr->ne(*items[0]))) { return Error::handle(name(), L"remove", Error::TEST, __FILE__, __LINE__); } if (char_ht_2.containsKey(keys[0])) { return Error::handle(name(), L"remove / containsKey", Error::TEST, __FILE__, __LINE__); } if (char_ht_2.containsValue(items[0])) { return Error::handle(name(), L"remove / containsValue", Error::TEST, __FILE__, __LINE__); } char_ht_2.remove(keys[1]); if (char_ht_2.containsKey(keys[1])) { return Error::handle(name(), L"remove / containsKey", Error::TEST, __FILE__, __LINE__); } if (char_ht_2.containsValue(items[1])) { return Error::handle(name(), L"remove / containsValue", Error::TEST, __FILE__, __LINE__); } // determine the occupancy of the htable (SYSTEM) // if (char_ht_0.isEmpty()) { return Error::handle(name(), L"isEmpty", Error::TEST, __FILE__, __LINE__); } // determine the occupancy of the htable (USER) // if (char_ht_2.isEmpty()) { return Error::handle(name(), L"isEmpty", Error::TEST, __FILE__, __LINE__); } // test clear (USER) -- first make a copy of char_ht_0 in system mode // char_ht_0.setAllocationMode(USER); char_ht_2.assign(char_ht_0); char_ht_0.setAllocationMode(SYSTEM); char_ht_2.clear(); if (!char_ht_2.isEmpty()) { return Error::handle(name(), L"clear", Error::TEST, __FILE__, __LINE__); } // test clear (SYSTEM) // char_ht_0.clear(); // create a hash table whose capacity* load_factor is less than 20 (SYSTEM) // HashTable<String, Char> tmp_htable_4(SYSTEM, (long)20); // put characters in the hash table // for (long i = 0; i < num_elem; i++) { // add the character to the htable // tmp_htable_4.insert(keys[i], items[i]); } // create a hash table whose capacity* load_factor is less than 20 (USER) // HashTable<String, Char> tmp_htable_7(USER, (long)20); tmp_htable_7.setLoadFactor(0.5); // put characters in the hash table // for (long i = 0; i < num_elem; i++) { // add the character to the htable // tmp_htable_7.insert(keys[i], items[i]); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // -------------------------------------------------------------------- // // 4. testing class-specific public methods // hash table property methods // // -------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: hash table property methods...\n"); Console::increaseIndention(); } // the capacity should have be increased automatically // if (tmp_htable_4.getCapacity() != 40) { tmp_htable_4.debug(L"rehash tmp_htable_4"); return Error::handle(name(), L"rehash", Error::TEST, __FILE__, __LINE__); } // check if all the keys are in the rehashed table // for (long i = 0; i < num_elem; i++) { if (!tmp_htable_4.containsKey(keys[i])) { return Error::handle(name(), L"rehash", Error::TEST, __FILE__, __LINE__); } } // clear the table // tmp_htable_4.clear(); if (tmp_htable_4.getNumItems() != 0) { return Error::handle(name(), L"clear", Error::TEST, __FILE__, __LINE__); } // the capacity should have be increased automatically // if (tmp_htable_7.getCapacity() != 40) { tmp_htable_7.debug(L"rehash tmp_htable_7"); return Error::handle(name(), L"rehash", Error::TEST, __FILE__, __LINE__); } // check if all the keys are in the rehashed table // for (long i = 0; i < num_elem; i++) { if (!tmp_htable_7.containsKey(keys[i])) { return Error::handle(name(), L"rehash", Error::TEST, __FILE__, __LINE__); } } // clear the table // tmp_htable_7.clear(); if (tmp_htable_7.getNumItems() != 0) { return Error::handle(name(), L"clear", Error::TEST, __FILE__, __LINE__); } // preform some stress test on the hash table in SYSTEM mode // HashTable<String, Long> int_hash; long int_hash_size = 1000; for (long i = 0; i < int_hash_size; i++) { String key; key.assign(i); key.insert(L"key of ", 0); Long int_obj(i * 3); int_hash.insert(key, &int_obj); } // find out the maximum number of elements in one hash // double max = 0; for (long i = 0; i < int_hash.getCapacity(); i++) { max = Integral::max(max, (double)int_hash.table_d(i).length()); } // make sure that the maximum search length is two orders of // magnitude less than the number of elements // if (max > ((double)int_hash_size * 0.01)) { return Error::handle(name(), L"hash", Error::TEST, __FILE__, __LINE__); } // read the values back out of the hash // for (long i = 0; i < int_hash_size; i++) { String key; key.assign(i); key.insert(L"key of ", 0); if (!int_hash.get(key)->eq(i * 3)) { return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } } // remove things from the hash and check that the size decreases // if (int_hash.getCapacity() != 2048) { return Error::handle(name(), L"rehash", Error::TEST, __FILE__, __LINE__); } for (long i = 0; i < 850; i++) { String key; key.assign(i); key.insert(L"key of ", 0); if (!int_hash.remove(key)) { return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } } if (int_hash.getCapacity() != 1024) { return Error::handle(name(), L"rehash", Error::TEST, __FILE__, __LINE__); } for (long i = 850; i < 900; i++) { String key; key.assign(i); key.insert(L"key of ", 0); if (!int_hash.remove(key)) { return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } } if (int_hash.getCapacity() != 512) { return Error::handle(name(), L"rehash", Error::TEST, __FILE__, __LINE__); } for (long i = 900; i < 975; i++) { String key; key.assign(i); key.insert(L"key of ", 0); if (!int_hash.remove(key)) { return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } } if (int_hash.getCapacity() != 128) { return Error::handle(name(), L"rehash", Error::TEST, __FILE__, __LINE__); } for (long i = 975; i < 999; i++) { String key; key.assign(i); key.insert(L"key of ", 0); if (!int_hash.remove(key)) { return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } } if (int_hash.getCapacity() != 128) { return Error::handle(name(), L"rehash", Error::TEST, __FILE__, __LINE__); } // preform some stress test on the hash table in USER mode // int_hash.clear(); int_hash.setCapacity(16); int_hash.setAllocationMode(USER); for (long i = 0; i < int_hash_size; i++) { String key; key.assign(i); key.insert(L"key of ", 0); Long* int_obj = new Long(i * 3); int_hash.insert(key, int_obj); } // read the values back out of the hash // for (long i = 0; i < int_hash_size; i++) { String key; key.assign(i); key.insert(L"key of ", 0); if (!int_hash.get(key)->eq(i * 3)) { return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } } // cleanup the USER mode hashtable // int_hash.clear(Integral::FREE); // cleanUp // for (long i = 0; i < num_elem; i++) { delete items[i]; } delete [] items; delete [] keys; // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------- // // 5. print completion message // //--------------------------------------------------------------------- // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } if (level_a > Integral::NONE) { SysString output(L"diagnostics passed for class "); output.concat(name()); output.concat(L"\n"); Console::put(output); } // exit gracefully // return true;}// end of include file//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -