SpaceCreator

stress_test_for_hash

Dec 18th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <ctime>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. vector<string> dictionary = {"how", "like", "how", "like", "a", "winter", "hath", "my", "absence", "been",
  10.                              "from", "thee", "the", "pleasure", "of", "the", "fleeting", "year",
  11.                              "what", "freezings", "have", "I", "felt,", "what", "dark", "days", "seen!", "what", "old",
  12.                              "decembers", "bareness", "every", "where",
  13.                              "and", "yet", "this", "time", "removed", "was", "summers", "time",
  14.                              "the", "teeming", "autumn", "big", "with", "rich", "increase",
  15.                              "bearing", "the", "wanton", "burthen", "of", "the", "prime",
  16.                              "like", "widowed", "wombs", "after", "their", "lords", "decease",
  17.                              "yet", "this", "abundant", "issue", "seem'd", "to", "me",
  18.                              "but", "hope", "of", "orphans,", "and", "unfathered", "fruit",
  19.                              "for", "summer", "and", "his", "pleasures", "wait", "on", "thee",
  20.                              "and", "thou", "away,", "the", "very", "birds", "are", "mute",
  21.                              "or", "if", "they", "sing,", "'tis", "with", "so", "dull", "a", "cheer",
  22.                              "that", "leaves", "look", "pale,", "dreading", "the", "winter's", "near",
  23.                              "a", "winter", "hath", "my", "absence", "been",
  24.                              "what", "freezings", "have", "I", "felt,", "what", "dark", "days", "seen!",
  25.                              "what", "old", "December's", "bareness", "every", "where",
  26.                              "and", "yet", "this", "time", "removed", "was", "summer's", "time",
  27.                              "the", "teeming", "autumn", "big", "with", "rich", "increase",
  28.                              "bearing", "the", "wanton", "burthen", "of", "the", "prime",
  29.                              "like", "widowed", "wombs", "after", "their", "lords", "decease",
  30.                              "yet", "this", "abundant", "issue", "seem'd", "to", "me",
  31.                              "but", "hope", "of", "orphans,", "and", "unfathered", "fruit",
  32.                              "for", "summer", "and", "his", "pleasures", "wait", "on", "thee",
  33.                              "and", "thou", "away,", "the", "very", "birds", "are", "mute",
  34.                              "or", "if", "they", "sing,", "'tis", "with", "so", "dull", "a", "cheer",
  35.                              "that", "leaves", "look", "pale,", "dreading", "the", "winter's", "near"};
  36.  
  37. bool add_map(string key, map<string, bool> &table) {
  38.     if (table[key] != true) {
  39.         table[key] = true;//added
  40.         return true;
  41.     } else {
  42.         return false;
  43.     }
  44. }
  45.  
  46. bool find_in_map(string key, map<string, bool> &table) {
  47.     return table[key] == true;
  48. }
  49.  
  50. bool delete_from_map(string key, map<string, bool> &table) {
  51.     if (table[key] == true) {
  52.         table[key] = false;//deleted
  53.         return true;
  54.     } else {
  55.         return false;
  56.     }
  57. }
  58.  
  59. string random_command() {
  60.     string str;
  61.     size_t sign = rand() % 2;
  62.     str += sign == 0 ? "+" : sign == 1 ? "-" : "?";
  63.     return str;
  64. }
  65.  
  66. string random_string() {
  67.     size_t dictionary_size = dictionary.size();
  68.     size_t num = rand() % dictionary_size;
  69.     num = num == 0 ? 1 : num;
  70.     string str = dictionary[num];
  71.     return str;
  72. }
  73.  
  74. void stress_test(int n_iter) {
  75.     for(int k = 0; k< n_iter;++k) {
  76.         cout << "STRESS No " << n_iter;
  77.         //hash_table *table = new hash_table;  - создаешь свою табличку
  78.         map<string, bool> map_table;
  79.         string key;
  80.         string command;
  81.         bool map_res = false;
  82.         bool hash_res = false;
  83.  
  84.         int num_of_words = rand() % 15 + 1; // вместо 15 - любое удобное число
  85.         for (int i = 0; i < num_of_words; ++i) {
  86.             command = random_command();
  87.             key = random_string();
  88.             cout << "Input : " << command + " " + key << endl;
  89.             hash_res = command == "+" ? /*функция добавления элемента*/(key) : command == "?" ? /*поиска*/(key)
  90.                                                                                               : /*удаления*/(key); //функции возвращают                bool
  91.             map_res = command == "+" ? add_map(key, map_table) : command == "?" ? find_in_map(key, map_table)
  92.                                                                                 : delete_from_map(key, map_table);
  93.             if (hash_res != map_res) {
  94.                 cout << "ASSERT TEST FAILED" << endl;
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment