void test_maplinearStore_given_Ali_Zorro_and_Sefia_should_add_to_the_next_bucket_and_full_should_throw_error(){ CEXCEPTION_T e; Person *Ali = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); Person *Sefia = personNew("Sefia",55,48.4); Person *Kikuri = personNew("Kikuri",45,42.4); Map *map = mapNew(5); map->bucket[3] = Ali; map->bucket[4] = Zorro; map->bucket[5] = Sefia; hash_ExpectAndReturn(Kikuri,3); Try{ maplinearStore(map , Kikuri ,comparePerson, hash); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_NOT_NULL(map->bucket[4]); TEST_ASSERT_NOT_NULL(map->bucket[5]); TEST_ASSERT_EQUAL_Person(Ali,map->bucket[3]); TEST_ASSERT_EQUAL_Person(Zorro,map->bucket[4]); TEST_ASSERT_EQUAL_Person(Sefia,map->bucket[5]); TEST_ASSERT_EQUAL_Person(Ali,getPersonFromBucketLinear(map->bucket[3])); TEST_ASSERT_EQUAL_Person(Zorro,getPersonFromBucketLinear(map->bucket[4])); TEST_ASSERT_EQUAL_Person(Sefia,getPersonFromBucketLinear(map->bucket[5])); } Catch(e){ TEST_FAIL_MESSAGE("Expect ERROR"); TEST_ASSERT_EQUAL(ERROR, e); TEST_ASSERT_NULL(map->bucket[6]); } }
void test_mapRemove_given_Ali_and_ali_is_in_the_list_of_3_element_in_the_map_and_should_remove_Ali_and_return_it_to_caller(){ Person *Ali = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); Person *Kikuri = personNew("Kikuri",48,46.4); Person *person; List *list = listNew(Ali, NULL); list = listAdd(Zorro,list); list = listAdd(Kikuri,list); Map *map = mapNew(5); map->bucket[3] = list; hash_ExpectAndReturn(Kikuri,3); // listDump(list, personDump); person = mapRemove(map , Kikuri ,comparePerson, hash); // listDump(list, personDump); TEST_ASSERT_NULL(person); TEST_ASSERT_NULL(getPersonFromBucket(map->bucket[3])); TEST_ASSERT_NOT_NULL(getPersonFromBucket(((List *)map->bucket[3])->next)); TEST_ASSERT_NOT_NULL(getPersonFromBucket(((List *)map->bucket[3])->next->next)); TEST_ASSERT_EQUAL_Person(Zorro,getPersonFromBucket(((List *)map->bucket[3])->next)); TEST_ASSERT_EQUAL_Person(Ali,getPersonFromBucket(((List *)map->bucket[3])->next->next)); }
void test_maplinearRemove_given_Ali_and_ali_is_not_in_the_linkedList_in_the_map_should_return_NULL4(){ Person *Ali = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); Person *Kikuri = personNew("Kikuri",50,40.4); Person *person; Map *map = mapNew(5); map->bucket[3] = Ali; map->bucket[4] = Zorro; hash_ExpectAndReturn(Kikuri,3); // personDump(map->bucket[3]); // personDump(map->bucket[4]); // personDump(map->bucket[5]); person = maplinearRemove(map , Kikuri ,comparePerson, hash); // personDump(map->bucket[3]); // personDump(map->bucket[4]); // personDump(map->bucket[5]); TEST_ASSERT_NULL(person); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_NOT_NULL(map->bucket[4]); TEST_ASSERT_NOT_NULL(getPersonFromBucketLinear(map->bucket[3])); TEST_ASSERT_NOT_NULL(getPersonFromBucketLinear(map->bucket[4])); TEST_ASSERT_EQUAL_Person(Ali,getPersonFromBucketLinear(map->bucket[3])); TEST_ASSERT_EQUAL_Person(Zorro,getPersonFromBucketLinear(map->bucket[4])); }
void test_maplinearRemove_given_Ali_and_ali_is_in_the_list_of_3_element_in_the_map_and_should_remove_Ali_and_return_it_to_caller(){ Person *Ali = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); Person *Kikuri = personNew("Kikuri",48,46.4); Person *person; Map *map = mapNew(6); map->bucket[3] = Ali; map->bucket[4] = Zorro; map->bucket[5] = Kikuri; hash_ExpectAndReturn(Kikuri,3); // personDump(map->bucket[3]); // personDump(map->bucket[4]); // personDump(map->bucket[5]); // printf("beforeremove\n"); person = maplinearRemove(map , Kikuri ,comparePerson, hash); // printf("afterremove\n"); // personDump(map->bucket[3]); // personDump(map->bucket[4]); // personDump(map->bucket[5]); TEST_ASSERT_NOT_NULL(person); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_NOT_NULL(map->bucket[4]); TEST_ASSERT_NULL(map->bucket[5]); TEST_ASSERT_EQUAL_Person(Kikuri,person); TEST_ASSERT_EQUAL_Person(Ali,getPersonFromBucketLinear(map->bucket[3])); TEST_ASSERT_EQUAL_Person(Zorro,getPersonFromBucketLinear(map->bucket[4])); }
/** * Case 1 */ void test_mapNew_given_length_of_10_should_create_a_new_Map(void) { Map *map = mapNew(10); TEST_ASSERT_NOT_NULL(map); TEST_ASSERT_NOT_NULL(map->bucket); TEST_ASSERT_EQUAL(10, map->length); TEST_ASSERT_EQUAL(0, map->size); }
void test_maplinearFind_given_Ali_and_ali_is_not_in_the_Map_should_return_NULL_since_no_Ali_object_in_Map2(){ Person *Ali = personNew("Ali",25,70.3); Person *person; Map *map = mapNew(5); // map->bucket[5] = Ali; // no add hash_ExpectAndReturn(Ali,5); person = maplinearFind(map , Ali ,comparePerson, hash); TEST_ASSERT_NULL(person); }
///////////////////////// // MAP LINEAR FIND // ///////////////////////// void test_maplinearFind_given_Ali_and_ali_is_in_the_Map_should_return_Ali_object1(){ Person *Ali = personNew("Ali",25,70.3); Person *person; Map *map = mapNew(5); map->bucket[5] = Ali; hash_ExpectAndReturn(Ali,5); person = maplinearFind(map , Ali ,comparePerson, hash); TEST_ASSERT_NOT_NULL(map->bucket[5]); TEST_ASSERT_EQUAL_Person(Ali,getPersonFromBucketLinear(map->bucket[5])); }
/** * Case 2 * * Add Ali into an empty map. Ali is first hashed and hash value 3 is obtained. * Ali is then placed into bucket 3. */ void test_mapStore_given_Ali_should_add_it_to_map(void) { Person *person = personNew("Ali", 25, 70.3); Map *map = mapNew(5); hash_ExpectAndReturn(person, 3); mapStore(map, person, comparePerson, hash); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_NOT_NULL(((Person *)((List *)map->bucket[3])->data)->name); TEST_ASSERT_EQUAL_Person(person, getPersonFromBucket(map->bucket[3])); }
void test_mapRemove_given_Ali_and_ali_is_not_in_the_Map_should_return_NULL_since_no_Ali_object_in_Map2(){ Person *Ali = personNew("Ali",25,70.3); List *list = listNew(Ali, NULL); Map *map = mapNew(5); Person *person; // map->bucket[3] = list; // no add hash_ExpectAndReturn(Ali,3); comparePerson(Ali,Ali); person = mapRemove(map , Ali ,comparePerson, hash); TEST_ASSERT_NULL(map->bucket[3]); TEST_ASSERT_NULL(person); }
void test_maplinearFind_given_Ali_and_ali_is_not_in_the_linkedList_in_the_map_should_return_NULL4(){ Person *Ali = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); Person *Abu = personNew("Abu",60,55.4); Person *person; Map *map = mapNew(6); map->bucket[3] = Zorro; map->bucket[4] = Abu; hash_ExpectAndReturn(Ali,3); person = maplinearFind(map , Ali ,comparePerson, hash); TEST_ASSERT_NULL(person); }
//////////////////// // MAP REMOVE // //////////////////// void test_mapRemove_given_Ali_and_ali_is_in_the_Map_should_return_Ali_object1(){ Person *Ali = personNew("Ali",25,70.3); List *list = listNew(Ali, NULL); Map *map = mapNew(5); Person *person; map->bucket[3] = list; hash_ExpectAndReturn(Ali,3); person = mapRemove(map , Ali ,comparePerson, hash); // listDump(list, personDump); TEST_ASSERT_NULL(person); TEST_ASSERT_NULL(getPersonFromBucket(map->bucket[3])); }
void test_mapFind_given_Ali_and_ali_is_not_in_the_linkedList_in_the_map_should_return_NULL4(){ Person *Ali = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); Person *Abu = personNew("Abu",60,55.4); Person *person; List *list = listNew(Abu, NULL); list = listAdd(Zorro,list); Map *map = mapNew(5); map->bucket[5] = list; hash_ExpectAndReturn(Ali,5); person = mapFind(map , Ali ,comparePerson, hash); TEST_ASSERT_NULL(person); }
void test_maplinearStore_given_Ali_but_Ali_is_same_in_Map(){ CEXCEPTION_T e; Person *Ali = personNew("Ali",25,70.3); Map *map = mapNew(5); map->bucket[3] =Ali; hash_ExpectAndReturn(Ali,3); comparePerson(Ali,Ali); Try{ maplinearStore(map , Ali ,comparePerson, hash); TEST_FAIL_MESSAGE("Expect ERR_SAME_ELEMENT"); } Catch(e){ TEST_ASSERT_EQUAL(ERR_SAME_ELEMENT, e); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_EQUAL_Person(Ali,getPersonFromBucketLinear(map->bucket[3])); } }
void test_mapStore_given_Ali_but_Ali_is_same_in_Map(){ CEXCEPTION_T e; Person *person = personNew("Ali",25,70.3); List *list = listNew(person, NULL); Map *map = mapNew(5); map->bucket[3] =list; hash_ExpectAndReturn(person,3); comparePerson(person,person); Try{ mapStore(map , person ,comparePerson, hash); TEST_FAIL_MESSAGE("Expect ERR_SAME_ELEMENT"); } Catch(e){ TEST_ASSERT_EQUAL(ERR_SAME_ELEMENT, e); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_EQUAL_Person(person,getPersonFromBucket(map->bucket[3])); } }
void test_mapStore_given_Zorro_added_into_Ali_in_the_Map(){ CEXCEPTION_T e; Person *person = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); List *list = listNew(person, NULL); Map *map = mapNew(5); map->bucket[3] = list; hash_ExpectAndReturn(Zorro,3); Try{ mapStore(map , Zorro ,comparePerson, hash); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_EQUAL_Person(Zorro,getPersonFromBucket(map->bucket[3])); TEST_ASSERT_EQUAL_Person(person,getPersonFromBucket(((List *)map->bucket[3])->next)); } Catch(e){ TEST_FAIL_MESSAGE("Expect Zorro"); } }
void test_mapRemove_given_Ali_and_ali_is_not_in_the_linkedList_in_the_map_should_return_NULL4(){ Person *Ali = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); Person *Kikuri = personNew("Kikuri",50,40.4); Person *person; List *list = listNew(Kikuri, NULL); list = listAdd(Zorro,list); Map *map = mapNew(5); map->bucket[3] = list; hash_ExpectAndReturn(Ali,3); // listDump(list, personDump); person = mapRemove(map , Ali ,comparePerson, hash); // listDump(list, personDump); TEST_ASSERT_NULL(person); TEST_ASSERT_NOT_NULL(getPersonFromBucket(map->bucket[3])); }
/** * Case 3 * * Add Ali into a map. Ali is first hashed and hash value 3 is obtained. * Ali is then placed into bucket 3. * But there is already a person called "Ali". */ void test_mapStore_given_Ali_should_throw_exception_when_there_is_already_an_Ali_stored(){ CEXCEPTION_T e; Person *person = personNew("Ali", 25, 70.3); List *list = listNew(person, NULL); Map *map = mapNew(5); // hash_ExpectAndReturn(person,3); // mapStore(map, person, comparePerson, hash); map->bucket[3] = list; hash_ExpectAndReturn(person,3); comparePerson_ExpectAndReturn(person, person, 1); Try{ mapStore(map, person, comparePerson, hash); TEST_FAIL_MESSAGE("Expect throw exception but did not."); } Catch(e){ TEST_ASSERT_EQUAL(ERR_SAME_ELEMENT, e); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_EQUAL_Person(person, getPersonFromBucket(map->bucket[3])); } }
void test_maplinearStore_given_Ali_and_Zorro_should_add_to_the_next_bucket(){ CEXCEPTION_T e; Person *Ali = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); Map *map = mapNew(5); map->bucket[3] = Ali; hash_ExpectAndReturn(Zorro,3); // comparePerson(Ali,Zorro); Try{ maplinearStore(map , Zorro ,comparePerson, hash); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_NOT_NULL(map->bucket[4]); TEST_ASSERT_EQUAL_Person(Ali,map->bucket[3]); TEST_ASSERT_EQUAL_Person(Zorro,map->bucket[4]); TEST_ASSERT_EQUAL_Person(Ali,getPersonFromBucketLinear(map->bucket[3])); TEST_ASSERT_EQUAL_Person(Zorro,getPersonFromBucketLinear(map->bucket[4])); } Catch(e){ TEST_FAIL_MESSAGE("Expect Zorro"); } }
/** * Case 4 * * Add Zorro into a map. Zorro is first hashed and hash value 3 is obtained. * Ali is already placed in the bucket 3. * Zorro will then be added to the head of the bucket 3. */ void test_mapStore_given_Zorro_should_add_into_the_head_when_there_is_already_an_Ali_stored(){ CEXCEPTION_T e; Person *person1 = personNew("Ali", 25, 70.3); Person *person2 = personNew("Zorro", 60, 70.3); List *list = listNew(person1, NULL); Map *map = mapNew(5); // hash_ExpectAndReturn(person,3); // mapStore(map, person, comparePerson, hash); map->bucket[3] = list; hash_ExpectAndReturn(person2,3); comparePerson_ExpectAndReturn(person1, person2, 0); Try{ mapStore(map, person2, comparePerson, hash); TEST_ASSERT_NOT_NULL(map->bucket[3]); TEST_ASSERT_EQUAL_Person(person2, getPersonFromBucket(map->bucket[3])); TEST_ASSERT_EQUAL_Person(person1, getPersonFromBucket(( (List *) map->bucket[3] )->next) ); } Catch(e){ TEST_FAIL_MESSAGE("Expect not to throw exception but thrown."); } }
void test_mapRemove_given_Ali_and_ali_is_in_the_linkedList_in_the_map_should_return_Ali_object3(){ Person *Ali = personNew("Ali",25,70.3); Person *Zorro = personNew("Zorro",60,55.4); Person *person; List *list = listNew(Ali, NULL); list = listAdd(Zorro,list); Map *map = mapNew(5); map->bucket[3] = list; hash_ExpectAndReturn(Zorro,3); // listDump(list, personDump); person = mapRemove(map , Zorro ,comparePerson, hash); // listDump(list, personDump); TEST_ASSERT_NULL(person); TEST_ASSERT_NULL(getPersonFromBucket(map->bucket[3])); TEST_ASSERT_NOT_NULL(getPersonFromBucket(((List *)map->bucket[3])->next)); TEST_ASSERT_EQUAL_Person(Ali,getPersonFromBucket(((List *)map->bucket[3])->next)); }