Exemplo n.º 1
0
TEST(HashTableTest, expandKey) {
	
	HashTable<std::string,std::string> ht;
	char buf[10];
	std::string c;

	for(int i = 0; i < 100; i ++) 
	{
		sprintf(buf,"a%d",i);
		//ht.add(buf, "ab");
		ht[buf] = "ab";
		ht[buf] = "ac";
		c = ht[buf];
		std::cout << "buf:" << buf << ",value:" << ht[buf] << ",c:" << c << std::endl;
	}

	//std::cout << "11:" << ht["11"]<< std::endl;
	
	EXPECT_EQ(100UL,ht.count());
	
	ht.remove("a8");

	EXPECT_EQ(99UL,ht.count());
	//EXPECT_TRUE(ht["a13"] == "ac");
	EXPECT_TRUE(ht.containsKey("a13"));
	EXPECT_TRUE(ht.contains("a13","ac"));
	EXPECT_FALSE(ht.containsKey("a1012"));
	EXPECT_FALSE(ht.contains("a1123","ac"));
	EXPECT_FALSE(ht.contains("a12","ab"));

	for(int i = 0; i < 1000; i ++) 
	{
		sprintf(buf,"a%d",i);
		//ht.add(buf, "ab");
		ht[buf] = "ab";
		ht[buf] = "ac";
		c = ht[buf];
		std::cout << "buf:" << buf << ",value:" << ht[buf] << ",c:" << c << std::endl;
	}

	ht.remove("a18");

	EXPECT_EQ(999UL,ht.count());
	//EXPECT_TRUE(ht["a13"] == "ac");
	EXPECT_TRUE(ht.containsKey("a13"));
	EXPECT_TRUE(ht.contains("a13","ac"));
	EXPECT_TRUE(ht.containsKey("a112"));
	EXPECT_FALSE(ht.contains("a1203","ac"));
	
}
Exemplo n.º 2
0
TEST(HashTableTest, IntKey)  {
	
	HashTable<uint,uint,IntHash> ht;
	
	EXPECT_EQ(0UL,ht.count());
	EXPECT_FALSE(ht.containsKey(1));
	ht.add(1,1);
	EXPECT_EQ(1UL,ht.count());
	EXPECT_TRUE(ht.containsKey(1));
	EXPECT_TRUE(ht.contains(1,1));
	ht[1] = 0;
	EXPECT_TRUE(ht[1] == 0U);
	ht.remove(1);
	EXPECT_EQ(0UL,ht.count());
}
//option 1, check a play to see if it is legal
void checkPlay(HashTable<string,int> dictionary) {
  cout << "\nEnter the word you wish to play: ";
  string word;
  cin >> word; //getting the word from user
  word = lowercase(word); //make word lowercase
  if (dictionary.containsKey(word)) { //check if it is in the dictionary or not
    cout << "Congratulations! You have found a legal play!\n" << endl;
  } else {
    cout << "Sorry, that is not a legal play.\n" << endl;
  }
  menu(dictionary); //back to menu
}
int main() {

  cout << "\nWelcome to Scrabble Solver\n" << endl;

  HashTable<string,int> dictionary; //allocate dictionary
  ifstream inFile("/usr/local/doc/sowpods.txt");
  string word;
  int counter = 0;
  while (inFile >> word) {  //load the dictionary
    word = lowercase(word);
    dictionary.insert(word, counter);
    ++counter;
    assert(dictionary.containsKey(word));
  }

  menu(dictionary);

  return 0;
}
Exemplo n.º 5
0
int main(){

	HashTable<string,string> ht;
	string key="Key#",num,aa,bb,val="Value#";
	for(int i=0;i<16;i++){
		num = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
		aa = key + num;
		bb = val + num;
		ht.insert(aa,bb);
	}
	cout << ht.size()<<endl;

	ht.printAll();
	for(int i=0;i<16;i++){
		num = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
		aa = key + num;
		cout << ht._delete(aa)<<endl;
	}

	val = "second#";
	for(int i=20;i<30;i++){
		num = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
		aa = key + num;
		bb = val + num;
		ht.insert(aa,bb);
	}

	ht.printAll();
	cout << ht.size()<<endl;

	cout << (ht.containsKey("Key#22")?"Found\n":"NOT Found\n");
	cout << (ht.containsValue("second#22")?"Found\n":"NOT Found\n");

	ht.iterator();
	Entry<string,string>* entry;
	while(ht.hasNext()){
		entry = ht.next();
		cout<<"Key = "<< entry->getKey()<<" | Value = "<< entry->getValue()<<endl;
	}

	return 0;
}
Exemplo n.º 6
0
int main()
{
  // print my name and this assignment's title 
  cout << "LAB 10: Write, Test, and Apply The HashTable Class Template\n"; 
  cout << "Programmer: Jacky Chow\n"; 
  cout << "Editor(s) used: Notepad++\n"; 
  cout << "Compiler(s) used: Visual C++\n"; 
  cout << "File: " << __FILE__ << endl; 
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
  
  int n = 100;
  int arraySize;
  HashTable<string, TermSection, 1009> a(hashCode);
  
  cout << "Created HashTable<string, TermSection, 1009> a(hashCode)\n";
  cout << "a.capacity returns: " << a.capacity() << endl;
  assert(807 == a.capacity());
  cout << "Size should be 0. a.size returns: " << a.size() << endl << endl;
  assert(0 == a.size());
  char* token;
  char buf[1000];
  const char* const tab = "\t";
  int counter = 0;
  ifstream fin;
  fin.open("dvc-schedule.txt");
  if (!fin.good()) throw "I/O error";  
  
  while (fin.good())
  {
    //progress bar
    if(counter == n) break;
  
    // read the line
    string line;
    getline(fin, line);
    strcpy(buf, line.c_str());
    if (buf[0] == 0) continue;
  
    // parse the line
    const string term(token = strtok(buf, tab));
    const string section(token = strtok(0, tab));
    const string course((token = strtok(0, tab)) ? token : "");
    const string instructor((token = strtok(0, tab)) ? token : "");
    const string whenWhere((token = strtok(0, tab)) ? token : "");
    if (course.find('-') == string::npos) continue; // invalid line
    const string subjectCode(course.begin(), course.begin() + course.find('-'));
    counter++;
    TermSection x = {term, section};
    a[x] = course;
  }
  fin.close();

  vector<TermSection> k = a.keys();  
  cout << "Capacity of a should now be 807. a.capacity returns: " << a.capacity() << endl;
  assert(807 == a.capacity());
  cout << "Size should also be 99. a.size returns: " << a.size() << endl;
  assert(99 == a.size());
  cout << "keys vector size should be equal to a.size: " << k.size() << endl;
  arraySize = k.size();
  assert(arraySize == a.size());
  counter = 0;

  // object copy testing
  {
    const HashTable<string, TermSection, 1009> copy = a;
    cout << endl;

    k = copy.keys();  
    cout << "Capacity of copy1 should be 807. copy.capacity returns: " << copy.capacity() << endl;
    assert(807 == copy.capacity());
    cout << "Size of copy1 should also be 99. copy.size returns: " << copy.size() << endl;
    assert(99 == copy.size());
    cout << "keys vector size of copy1 should be equal to copy.size: " << k.size() << endl;
    arraySize = k.size();
    assert(arraySize == copy.size());
    cout << "Printing out course from copy1 using element 4 in vector k: " << copy[k[4]] << endl;
    cout << "Now testing that copy.containsKey() will return true with that parameter.\n";
    if(copy.containsKey(k[4])) cout << "copy.containsKey(k[4]) returned True.\n";
    else cout << "Error! a.containsKey(k[4]) returned False.\n";
    assert(copy.containsKey(k[4]));
    TermSection y = {"Horch 2036", "9999"};
    cout << "Printing out course with fake y parameter(should be a dummy): " << copy[y] << endl;
    cout << "Now testing that copy.containsKey() will return false with fake y parameter.\n";
    if(!copy.containsKey(y)) cout << "copy.containsKey(y) returned False.\n";
    else cout << "Error! copy.containsKey(y) returned True.\n";  
    assert(!copy.containsKey(y));    
  }
  
  // object assignment testing
  {
    cout << endl;
    HashTable<string, TermSection, 1009> copy(hashCode); 
    cout << "Created copy 2 with a capacity 807.\nIt returns " << copy.capacity() << endl;
    assert(807 == copy.capacity());
    cout << "Copy 2 should have a size of 0. It returns: " << copy.size() << endl;
    cout << "Assigning copy 2 to equal a.\n";
    copy = a;
    k = copy.keys();  
    cout << "Capacity of copy2 should now be 160. copy.capacity returns: " << copy.capacity() << endl;
    assert(807 == copy.capacity());
    cout << "Size of copy2 should now be 99. copy.size returns: " << copy.size() << endl;
    assert(99 == copy.size());
    cout << "keys vector size of copy2 should be equal to copy.size: " << k.size() << endl;
    arraySize = k.size();
    assert(arraySize == copy.size());
    cout << "Using the clear function on copy 2.\n";
    copy.clear();
    cout << "Capacity of copy2 should still be 160. copy.capacity returns: " << copy.capacity() << endl;
    assert(807 == copy.capacity());
    cout << "Size of copy2 should now be 0. copy.size returns: " << copy.size() << endl;
    assert(0 == copy.size());
    k = copy.keys();
    cout << "keys vector size of copy2 should be equal to copy.size: " << k.size() << endl;
    arraySize = k.size();
    assert(arraySize == copy.size());
  }

  fin.open("dvc-schedule.txt");
  if (!fin.good()) throw "I/O error";  

  while (fin.good())
  {
    //progress bar
    if(counter == n) break;
  
    // read the line
    string line;
    getline(fin, line);
    strcpy(buf, line.c_str());
    if (buf[0] == 0) continue;
  
    // parse the line
    const string term(token = strtok(buf, tab));
    const string section(token = strtok(0, tab));
    const string course((token = strtok(0, tab)) ? token : "");
    const string instructor((token = strtok(0, tab)) ? token : "");
    const string whenWhere((token = strtok(0, tab)) ? token : "");
    if (course.find('-') == string::npos) continue; // invalid line
    const string subjectCode(course.begin(), course.begin() + course.find('-'));
    
    TermSection x = {term, section};      
    a.deleteKey(x);                     
    counter++;    
  } 
  fin.close();
  
  cout << "\nReopened the file to check and delete entries in array a.\n";
  k = a.keys();
  cout << "Capacity of a should still be 807. a.capacity returns: " << a.capacity() << endl;
  assert(807 == a.capacity());
  cout << "Size should also be 0. a.size returns: " << a.size() << endl;
  assert(0 == a.size());
  cout << "keys vector size should be equal to a.size: " << k.size() << endl;
  arraySize = k.size();
  assert(arraySize == a.size());
}