Advertisement
syxstep

Untitled

Feb 20th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include "test_runner.h"
  2. #include <limits>
  3. #include <random>
  4. #include <unordered_set>
  5. #include <tuple>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. struct Address {
  11.     string city, street;
  12.     int building;
  13.  
  14.     bool operator==(const Address& other) const {
  15.         return 1;
  16.     }
  17. };
  18.  
  19. struct Person {
  20.     string name;
  21.     int height;
  22.     double weight;
  23.     Address address;
  24.  
  25.     bool operator==(const Person& other) const {
  26.         return 1;
  27.     }
  28. };
  29.  
  30. struct AddressHasher { 
  31.    
  32.  
  33.  
  34.     size_t operator()(const Address& other) const
  35.     {
  36.        
  37.         return 0;
  38.     }
  39. };
  40.  
  41. struct PersonHasher {
  42.  
  43.     size_t operator()(const Person& other) const
  44.     {
  45.         return 0;
  46.     }
  47. };
  48.  
  49. const vector<string> WORDS = {
  50.   "Kieran", "Jong", "Jisheng", "Vickie", "Adam", "Simon", "Lance",
  51.   "Everett", "Bryan", "Timothy", "Daren", "Emmett", "Edwin", "List",
  52.   "Sharon", "Trying", "Dan", "Saad", "Kamiya", "Nikolai", "Del",
  53.   "Casper", "Arthur", "Mac", "Rajesh", "Belinda", "Robin", "Lenora",
  54.   "Carisa", "Penny", "Sabrina", "Ofer", "Suzanne", "Pria", "Magnus",
  55.   "Ralph", "Cathrin", "Phill", "Alex", "Reinhard", "Marsh", "Tandy",
  56.   "Mongo", "Matthieu", "Sundaresan", "Piotr", "Ramneek", "Lynne", "Erwin",
  57.   "Edgar", "Srikanth", "Kimberly", "Jingbai", "Lui", "Jussi", "Wilmer",
  58.   "Stuart", "Grant", "Hotta", "Stan", "Samir", "Ramadoss", "Narendra",
  59.   "Gill", "Jeff", "Raul", "Ken", "Rahul", "Max", "Agatha",
  60.   "Elizabeth", "Tai", "Ellen", "Matt", "Ian", "Toerless", "Naomi",
  61.   "Rodent", "Terrance", "Ethan", "Florian", "Rik", "Stanislaw", "Mott",
  62.   "Charlie", "Marguerite", "Hitoshi", "Panacea", "Dieter", "Randell", "Earle",
  63.   "Rajiv", "Ted", "Mann", "Bobbie", "Pat", "Olivier", "Harmon",
  64.   "Raman", "Justin"
  65. };
  66.  
  67. void TestSmoke() {
  68.     vector<Person> points = {
  69.       {"John", 180, 82.5, {"London", "Baker St", 221}},//здесь падает при {"London", "Baker St", 221} но если сделать Address{"London", "Baker St", 221},то все норм
  70.       {"Sherlock", 190, 75.3, {"London", "Baker St", 221}},
  71.     };
  72.  
  73.     unordered_set<Person, PersonHasher> point_set;
  74.     for (const auto& point : points) {
  75.         point_set.insert(point);
  76.     }
  77.  
  78.     ASSERT_EQUAL(points.size(), point_set.size());
  79.     for (const auto& point : points) {
  80.         ASSERT_EQUAL(point_set.count(point), static_cast<size_t>(1));
  81.     }
  82. }
  83.  
  84. void TestPurity() {
  85.     Person person = { "John", 180, 82.5, {"London", "Baker St", 221} };
  86.     PersonHasher hasher;
  87.  
  88.     auto hash = hasher(person);
  89.     for (size_t t = 0; t < 100; ++t) {
  90.         ASSERT_EQUAL(hasher(person), hash);
  91.     }
  92. };
  93.  
  94. void TestDistribution() {
  95.     auto seed = 42;
  96.     mt19937 gen(seed);
  97.  
  98.     uniform_int_distribution<int> height_dist(150, 200);
  99.     uniform_int_distribution<int> weight_dist(100, 240);  // [50, 120]
  100.     uniform_int_distribution<int> building_dist(1, 300);
  101.     uniform_int_distribution<int> word_dist(0, WORDS.size() - 1);
  102.  
  103.     PersonHasher hasher;
  104.  
  105.  
  106.     const size_t num_buckets = 2053;
  107.  
  108.     const size_t perfect_bucket_size = 50;
  109.     const size_t num_points = num_buckets * perfect_bucket_size;
  110.     vector<size_t> buckets(num_buckets);
  111.     for (size_t t = 0; t < num_points; ++t) {
  112.         Person person;
  113.         person.name = WORDS[word_dist(gen)];
  114.         person.height = height_dist(gen);
  115.         person.weight = weight_dist(gen) * 0.5;
  116.         person.address.city = WORDS[word_dist(gen)];
  117.         person.address.street = WORDS[word_dist(gen)];
  118.         person.address.building = building_dist(gen);
  119.         ++buckets[hasher(person) % num_buckets];
  120.     }
  121.  
  122.     double pearson_stat = 0;
  123.     for (auto bucket_size : buckets) {
  124.         size_t size_diff = bucket_size - perfect_bucket_size;
  125.         pearson_stat +=
  126.             size_diff * size_diff / static_cast<double>(perfect_bucket_size);
  127.     }
  128.     const double critical_value = 2158.4981036918693;
  129.     ASSERT(pearson_stat < critical_value);
  130. }
  131.  
  132. int main() {
  133.     TestRunner tr;
  134.     RUN_TEST(tr, TestSmoke);
  135.     RUN_TEST(tr, TestPurity);
  136.     RUN_TEST(tr, TestDistribution);
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement