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])); }
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])); }
/** Add Ali into an empty map. Ali is first hashed and hash value 3 is obtained and 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_EQUAL_STRING("Ali",((Person *)((List *)map->bucket[3])->data)->name); TEST_ASSERT_EQUAL_Person(person,(Person *)((List *)map->bucket[3])->data); TEST_ASSERT_EQUAL_Person(person,getPersonFromBucket(map->bucket[3])); // TEST_ASSERT_EQUAL_PERSON("Ali",25,70.3,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_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]); } }
///////////////////////// // 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])); }
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_maplinearFind_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; Map *map = mapNew(5); map->bucket[3] = Ali; map->bucket[4] = Zorro; hash_ExpectAndReturn(Ali,3); person = maplinearFind(map , Ali ,comparePerson, hash); TEST_ASSERT_NOT_NULL(person); TEST_ASSERT_EQUAL_Person(Ali,person); }
void test_mapFind_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[5] = list; hash_ExpectAndReturn(Ali,5); person = mapFind(map , Ali ,comparePerson, hash); TEST_ASSERT_NOT_NULL(person); TEST_ASSERT_EQUAL_Person(Ali,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])); } }
/** * 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_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)); }