Skip to content

Commit

Permalink
Update: Stack and Hashtable test: do not use large stack sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
patham9 committed Nov 4, 2022
1 parent 146cb05 commit a5bada0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int Shell_ProcessInput(char *line)
//trim string, for IRC etc. convenience
for(int i=strlen(line)-1; i>=0; i--)
{
if(!isspace(line[i]))
if(!isspace((int) line[i]))
{
break;
}
Expand Down
46 changes: 24 additions & 22 deletions src/unit_tests/HashTable_Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@
* THE SOFTWARE.
*/


#define HASHTABLE_TEST_STRUCTURE_SIZE 10
void HashTable_Test()
{
HashTable HTtest;
VMItem* HTest_storageptrs[CONCEPTS_MAX];
VMItem HTest_storage[CONCEPTS_MAX];
VMItem* HTest_HT[CONCEPTS_HASHTABLE_BUCKETS]; //the hash of the concept term is the index
VMItem* HTest_storageptrs[HASHTABLE_TEST_STRUCTURE_SIZE];
VMItem HTest_storage[HASHTABLE_TEST_STRUCTURE_SIZE];
VMItem* HTest_HT[HASHTABLE_TEST_STRUCTURE_SIZE]; //the hash of the concept term is the index
puts(">>HashTable test start");
HashTable_INIT(&HTtest, HTest_storage, HTest_storageptrs, HTest_HT, CONCEPTS_HASHTABLE_BUCKETS, CONCEPTS_MAX, (Equal) Term_Equal, (Hash) Term_Hash);
assert(HTtest.VMStack.stackpointer == CONCEPTS_MAX, "The stack should be full!");
HashTable_INIT(&HTtest, HTest_storage, HTest_storageptrs, HTest_HT, HASHTABLE_TEST_STRUCTURE_SIZE, HASHTABLE_TEST_STRUCTURE_SIZE, (Equal) Term_Equal, (Hash) Term_Hash);
assert(HTtest.VMStack.stackpointer == HASHTABLE_TEST_STRUCTURE_SIZE, "The stack should be full!");
//Insert a first concept:
Term term1 = Narsese_Term("<a --> b>");
Concept c1 = { .id = 1, .term = term1 };
HashTable_Set(&HTtest, &term1, &c1);
assert(HTtest.VMStack.stackpointer == CONCEPTS_MAX-1, "One item should be taken off of the stack");
assert(HTtest.HT[c1.term.hash % CONCEPTS_HASHTABLE_BUCKETS] != NULL, "Item didn't go in right place");
assert(HTtest.VMStack.stackpointer == HASHTABLE_TEST_STRUCTURE_SIZE-1, "One item should be taken off of the stack");
assert(HTtest.HT[c1.term.hash % HASHTABLE_TEST_STRUCTURE_SIZE] != NULL, "Item didn't go in right place");
//Return it
Concept *c1_returned = HashTable_Get(&HTtest, &term1);
assert(c1_returned != NULL, "Returned item is null (1)");
Expand All @@ -55,30 +57,30 @@ void HashTable_Test()
Concept c3 = { .id = 3, .term = term3 }; //use different term but same hash, hash collision!
HashTable_Set(&HTtest, &term3, &c3);
//there should be a chain of 3 concepts now at the hash position:
assert(Term_Equal(HTtest.HT[c1.term.hash % CONCEPTS_HASHTABLE_BUCKETS]->key, &c1.term), "c1 not there! (1)");
assert(Term_Equal(((VMItem*)HTtest.HT[c1.term.hash % CONCEPTS_HASHTABLE_BUCKETS]->next)->key, &c2.term), "c2 not there! (1)");
assert(Term_Equal(((VMItem*)((VMItem*)HTtest.HT[c1.term.hash % CONCEPTS_HASHTABLE_BUCKETS]->next)->next)->key, &c3.term), "c3 not there! (1)");
assert(Term_Equal(HTtest.HT[c1.term.hash % HASHTABLE_TEST_STRUCTURE_SIZE]->key, &c1.term), "c1 not there! (1)");
assert(Term_Equal(((VMItem*)HTtest.HT[c1.term.hash % HASHTABLE_TEST_STRUCTURE_SIZE]->next)->key, &c2.term), "c2 not there! (1)");
assert(Term_Equal(((VMItem*)((VMItem*)HTtest.HT[c1.term.hash % HASHTABLE_TEST_STRUCTURE_SIZE]->next)->next)->key, &c3.term), "c3 not there! (1)");
//Delete the middle one, c2
HashTable_Delete(&HTtest, &term2);
assert(((Concept*)((VMItem*)HTtest.HT[c1.term.hash % CONCEPTS_HASHTABLE_BUCKETS]->next)->value)->id == 3, "c3 not there according to id! (2)");
assert(Term_Equal(HTtest.HT[c1.term.hash % CONCEPTS_HASHTABLE_BUCKETS]->key, &c1.term), "c1 not there! (2)");
assert(Term_Equal(((VMItem*)HTtest.HT[c1.term.hash % CONCEPTS_HASHTABLE_BUCKETS]->next)->key, &c3.term), "c3 not there! (2)");
assert(((Concept*)((VMItem*)HTtest.HT[c1.term.hash % HASHTABLE_TEST_STRUCTURE_SIZE]->next)->value)->id == 3, "c3 not there according to id! (2)");
assert(Term_Equal(HTtest.HT[c1.term.hash % HASHTABLE_TEST_STRUCTURE_SIZE]->key, &c1.term), "c1 not there! (2)");
assert(Term_Equal(((VMItem*)HTtest.HT[c1.term.hash % HASHTABLE_TEST_STRUCTURE_SIZE]->next)->key, &c3.term), "c3 not there! (2)");
//Delete the last one, c3
HashTable_Delete(&HTtest, &term3);
assert(Term_Equal(HTtest.HT[c1.term.hash % CONCEPTS_HASHTABLE_BUCKETS]->key, &c1.term), "c1 not there! (3)");
assert(Term_Equal(HTtest.HT[c1.term.hash % HASHTABLE_TEST_STRUCTURE_SIZE]->key, &c1.term), "c1 not there! (3)");
//Delete the first one, which is the last one left, c1
HashTable_Delete(&HTtest, &term1);
assert(HTtest.HT[c1.term.hash % CONCEPTS_HASHTABLE_BUCKETS] == NULL, "Hash table at hash position must be null");
assert(HTtest.VMStack.stackpointer == CONCEPTS_MAX, "All elements should be free now");
assert(HTtest.HT[c1.term.hash % HASHTABLE_TEST_STRUCTURE_SIZE] == NULL, "Hash table at hash position must be null");
assert(HTtest.VMStack.stackpointer == HASHTABLE_TEST_STRUCTURE_SIZE, "All elements should be free now");
//test for chars:
HashTable HTtest2;
VMItem* HTtest2_storageptrs[ATOMS_MAX];
VMItem HTtest2_storage[ATOMS_MAX];
VMItem* HTtest2_HT[ATOMS_HASHTABLE_BUCKETS];
HashTable_INIT(&HTtest2, HTtest2_storage, HTtest2_storageptrs, HTtest2_HT, ATOMS_HASHTABLE_BUCKETS, ATOMS_MAX, (Equal) Narsese_StringEqual, (Hash) Narsese_StringHash);
VMItem* HTtest2_storageptrs[HASHTABLE_TEST_STRUCTURE_SIZE];
VMItem HTtest2_storage[HASHTABLE_TEST_STRUCTURE_SIZE];
VMItem* HTtest2_HT[HASHTABLE_TEST_STRUCTURE_SIZE];
HashTable_INIT(&HTtest2, HTtest2_storage, HTtest2_storageptrs, HTtest2_HT, HASHTABLE_TEST_STRUCTURE_SIZE, HASHTABLE_TEST_STRUCTURE_SIZE, (Equal) Narsese_StringEqual, (Hash) Narsese_StringHash);
char *testname = "test";
char blockname[ATOMIC_TERM_LEN_MAX] = {0};
strncpy(blockname, testname, ATOMIC_TERM_LEN_MAX-1);
char blockname[HASHTABLE_TEST_STRUCTURE_SIZE] = {0};
strncpy(blockname, testname, HASHTABLE_TEST_STRUCTURE_SIZE-1);
HashTable_Set(&HTtest2, blockname, (void*) 42);
long res = (long) HashTable_Get(&HTtest2, blockname);
assert(res == 42, "Result is not right!");
Expand Down
5 changes: 3 additions & 2 deletions src/unit_tests/Stack_Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
* THE SOFTWARE.
*/

#define STACK_TEST_STRUCTURE_SIZE 5
void Stack_Test()
{
puts(">>Stack test start");
Stack stack = {0};
VMItem* storageptrs[CONCEPTS_MAX];
Stack_INIT(&stack, (void**) storageptrs, CONCEPTS_MAX);
VMItem* storageptrs[STACK_TEST_STRUCTURE_SIZE];
Stack_INIT(&stack, (void**) storageptrs, STACK_TEST_STRUCTURE_SIZE);
Concept c1 = {0};
Concept c2 = {0};
VMItem item1 = { .value = &c1 };
Expand Down

0 comments on commit a5bada0

Please sign in to comment.