void test_hashMap_remove(void) { char * key = "key"; char * value = "value"; char * key2 = NULL; char * value2 = "value2"; char * removeKey; hashMap_clear(map, false, false); // Add one entry hashMap_put(map, key, value); // Add second entry with null key hashMap_put(map, key2, value2); // Remove unexisting entry for map removeKey = "unexisting"; hashMap_remove(map, removeKey); CU_ASSERT_EQUAL(map->size, 2); CU_ASSERT_FALSE(hashMap_isEmpty(map)); hashMap_remove(map, key); CU_ASSERT_EQUAL(map->size, 1); hashMap_remove(map, key2); CU_ASSERT_EQUAL(map->size, 0); CU_ASSERT_TRUE(hashMap_isEmpty(map)); // Remove unexisting entry for empty map removeKey = "unexisting"; hashMap_remove(map, removeKey); CU_ASSERT_EQUAL(map->size, 0); CU_ASSERT_TRUE(hashMap_isEmpty(map)); }
void test_hashMap_isEmpty(void) { char * key = "key"; char * value = "value"; hashMap_clear(map, false, false); CU_ASSERT_EQUAL(map->size, 0); CU_ASSERT_TRUE(hashMap_isEmpty(map)); // Add one entry hashMap_put(map, key, value); CU_ASSERT_EQUAL(map->size, 1); CU_ASSERT_FALSE(hashMap_isEmpty(map)); // Remove entry hashMap_remove(map, key); CU_ASSERT_EQUAL(map->size, 0); CU_ASSERT_TRUE(hashMap_isEmpty(map)); }
void test_hashMap_removeMapping(void) { char * key = "key"; char * value = "value"; char * key2 = NULL; char * value2 = "value2"; hash_map_entry_pt entry1; hash_map_entry_pt entry2; hash_map_entry_pt removed; hashMap_clear(map, false, false); // Add one entry hashMap_put(map, key, value); // Add second entry with null key hashMap_put(map, key2, value2); entry1 = hashMap_getEntry(map, key); entry2 = hashMap_getEntry(map, key2); CU_ASSERT_PTR_NOT_NULL_FATAL(entry1); CU_ASSERT_PTR_NOT_NULL_FATAL(entry2); removed = hashMap_removeMapping(map, entry1); CU_ASSERT_PTR_EQUAL(entry1, removed); CU_ASSERT_EQUAL(map->size, 1); removed = hashMap_removeMapping(map, entry2); CU_ASSERT_PTR_EQUAL(entry2, removed); CU_ASSERT_EQUAL(map->size, 0); // Remove unexisting entry for empty map hashMap_removeMapping(map, NULL); CU_ASSERT_EQUAL(map->size, 0); CU_ASSERT_TRUE(hashMap_isEmpty(map)); }
celix_status_t driverMatcher_getBestMatchInternal(driver_matcher_pt matcher, apr_pool_t *pool, match_pt *match) { celix_status_t status = CELIX_SUCCESS; if (!hashMap_isEmpty(matcher->attributes)) { match_key_t matchKey = NULL; hash_map_iterator_pt iter = hashMapIterator_create(matcher->attributes); while (hashMapIterator_hasNext(iter)) { hash_map_entry_pt entry = hashMapIterator_nextEntry(iter); match_key_t key = hashMapEntry_getKey(entry); if (matchKey == NULL || matchKey->matchValue < key->matchValue) { matchKey = key; } } hashMapIterator_destroy(iter); array_list_pt das = hashMap_get(matcher->attributes, matchKey); service_reference_pt best = NULL; int i; for (i = 0; i < arrayList_size(das); i++) { driver_attributes_pt attributes = arrayList_get(das, i); service_reference_pt reference = NULL; celix_status_t substatus = driverAttributes_getReference(attributes, &reference); if (substatus == CELIX_SUCCESS) { if (best != NULL) { printf("DRIVER_MATCHER: Compare ranking\n"); char *rank1Str, *rank2Str; int rank1, rank2; service_registration_pt registration = NULL; substatus = serviceReference_getServiceRegistration(reference, ®istration); if (substatus == CELIX_SUCCESS) { properties_pt properties = NULL; status = serviceRegistration_getProperties(registration, &properties); if (status == CELIX_SUCCESS) { rank1Str = properties_getWithDefault(properties, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, "0"); rank2Str = properties_getWithDefault(properties, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, "0"); rank1 = atoi(rank1Str); rank2 = atoi(rank2Str); if (rank1 != rank2) { if (rank1 > rank2) { best = reference; } } else { printf("DRIVER_MATCHER: Compare id's\n"); char *id1Str, *id2Str; long id1, id2; id1Str = properties_get(properties, (char *) OSGI_FRAMEWORK_SERVICE_ID); id2Str = properties_get(properties, (char *) OSGI_FRAMEWORK_SERVICE_ID); id1 = atol(id1Str); id2 = atol(id2Str); if (id1 < id2) { best = reference; } } } } } else { best = reference; } } } *match = apr_palloc(pool, sizeof(**match)); if (!*match) { status = CELIX_ENOMEM; } else { (*match)->matchValue = matchKey->matchValue; (*match)->reference = best; } } return status; }
celix_status_t driverMatcher_getBestMatchInternal(driver_matcher_pt matcher, match_pt *match) { celix_status_t status = CELIX_SUCCESS; if (!hashMap_isEmpty(matcher->attributes)) { match_key_t matchKey = NULL; hash_map_iterator_pt iter = hashMapIterator_create(matcher->attributes); while (hashMapIterator_hasNext(iter)) { hash_map_entry_pt entry = hashMapIterator_nextEntry(iter); match_key_t key = hashMapEntry_getKey(entry); if (matchKey == NULL || matchKey->matchValue < key->matchValue) { matchKey = key; } } hashMapIterator_destroy(iter); array_list_pt das = hashMap_get(matcher->attributes, matchKey); service_reference_pt best = NULL; int i; for (i = 0; i < arrayList_size(das); i++) { driver_attributes_pt attributes = arrayList_get(das, i); service_reference_pt reference = NULL; celix_status_t substatus = driverAttributes_getReference(attributes, &reference); if (substatus == CELIX_SUCCESS) { if (best != NULL) { char *rank1Str, *rank2Str; int rank1, rank2; rank1Str = "0"; rank2Str = "0"; logHelper_log(matcher->loghelper, OSGI_LOGSERVICE_DEBUG, "DRIVER_MATCHER: Compare ranking"); serviceReference_getProperty(reference, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, &rank1Str); serviceReference_getProperty(reference, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, &rank2Str); rank1 = atoi(rank1Str); rank2 = atoi(rank2Str); if (rank1 != rank2) { if (rank1 > rank2) { best = reference; } } else { char *id1Str, *id2Str; long id1, id2; id1Str = NULL; id2Str = NULL; logHelper_log(matcher->loghelper, OSGI_LOGSERVICE_DEBUG, "DRIVER_MATCHER: Compare id's"); serviceReference_getProperty(reference, (char *) OSGI_FRAMEWORK_SERVICE_ID, &id1Str); serviceReference_getProperty(reference, (char *) OSGI_FRAMEWORK_SERVICE_ID, &id2Str); id1 = atol(id1Str); id2 = atol(id2Str); if (id1 < id2) { best = reference; } } } else { best = reference; } } } *match = calloc(1, sizeof(**match)); if (!*match) { status = CELIX_ENOMEM; } else { (*match)->matchValue = matchKey->matchValue; (*match)->reference = best; } } return status; }