Esempio n. 1
0
void TEST_Construction() {
  Hashtable h;
  h.resize(26);
  Item a = {"a", 'a'};
  Item b = {"b", 'b'};
  Item c = {"c", 'c'};
  Item abcd1234 = {"abcd1234", 99};
  Item abcdefghijklmnopqrstuv = {"abcdefghijklmnopqrstuv", 101};

  insert(&h, &a);
  ASSERT_EQ(h[simple_hash::hash("a") % h.size()]->value,'a');
  insert(&h, &b);
  ASSERT_EQ(h[simple_hash::hash("b") % h.size()]->value,'b');
  insert(&h, &c);
  ASSERT_EQ(h[simple_hash::hash("c") % h.size()]->value,'c');
  insert(&h, &abcd1234);
  ASSERT_EQ(h[simple_hash::hash("abcd1234") % h.size()]->value, 'a');
  ASSERT_EQ(h[simple_hash::hash("abcd1234") % h.size() + 1]->value, 'b');
  ASSERT_EQ(h[simple_hash::hash("abcd1234") % h.size() + 2]->value, 'c');
  ASSERT_EQ(h[simple_hash::hash("abcd1234") % h.size() + 3]->value, 99);
  insert(&h, &abcdefghijklmnopqrstuv);
  ASSERT_EQ(h[simple_hash::hash("abcd1234") % h.size()]->value, 'a');
  ASSERT_EQ(h[simple_hash::hash("abcd1234") % h.size() + 1]->value, 'b');
  ASSERT_EQ(h[simple_hash::hash("abcd1234") % h.size() + 2]->value, 'c');
  ASSERT_EQ(h[simple_hash::hash("abcd1234") % h.size() + 3]->value, 99);
  ASSERT_EQ(h[simple_hash::hash("abcdefghijklmnopqrstuv") % h.size() + 4]->value, 101);
}
Esempio n. 2
0
void TEST_Resizing() {
  Hashtable h;
  h.resize(26);
  Item a = {"a", 'a'};
  Item b = {"b", 'b'};
  Item c = {"c", 'c'};
  Item abcd1234 = {"abcd1234", 99};
  Item abcdefghijklmnopqrstuv = {"abcdefghijklmnopqrstuv", 101};

  ASSERT_EQ(find(&h, "blah"), nullptr);

  insert(&h, &a);
  ASSERT_EQ(find(&h, a.key), &a);
  insert(&h, &b);
  ASSERT_EQ(find(&h, b.key), &b);
  insert(&h, &c);
  ASSERT_EQ(find(&h, c.key), &c);
  insert(&h, &abcd1234);
  ASSERT_EQ(find(&h, abcd1234.key), &abcd1234);
  insert(&h, &abcdefghijklmnopqrstuv);
  ASSERT_EQ(find(&h, abcdefghijklmnopqrstuv.key), &abcdefghijklmnopqrstuv);

  resize(&h, 52);
  ASSERT_EQ(h.size(), 52);
  ASSERT_EQ(find(&h, "blah"), nullptr);
  ASSERT_EQ(find(&h, a.key), &a);
  ASSERT_EQ(find(&h, b.key), &b);
  ASSERT_EQ(find(&h, c.key), &c);
  ASSERT_EQ(find(&h, abcd1234.key), &abcd1234);
  ASSERT_EQ(find(&h, abcdefghijklmnopqrstuv.key), &abcdefghijklmnopqrstuv);

  resize(&h, 13);
  ASSERT_EQ(h.size(), 13);
  ASSERT_EQ(find(&h, "blah"), nullptr);
  ASSERT_EQ(find(&h, a.key), &a);
  ASSERT_EQ(find(&h, b.key), &b);
  ASSERT_EQ(find(&h, c.key), &c);
  ASSERT_EQ(find(&h, abcd1234.key), &abcd1234);
  ASSERT_EQ(find(&h, abcdefghijklmnopqrstuv.key), &abcdefghijklmnopqrstuv);
}