libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

libutil/src/test/test_map.c (85 lines, 2.2 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
// test_map.c
// Map unit test for libutil
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence

#include "../map.h"
#include "../util.h"
#include "unit.h"

#include <stdio.h>
#include <string.h>

int main(void) {
	{
		map_initial_capacity = 4;
		map m = map_init(NULL);
		
		map_insert(&m, "key0", "value0");
		map_insert(&m, "key4", "value4");
		map_insert(&m, "key3", "value3");
		
		ASSERT("T000 INSERT AND LOOKUP STRING 0",
				strcmp(map_lookup(&m, "key0"), "value0") == 0);
		ASSERT("T001 INSERT AND LOOKUP STRING 1",
				strcmp(map_lookup(&m, "key4"), "value4") == 0);
		ASSERT("T002 INSERT AND LOOKUP STRING 2",
				strcmp(map_lookup(&m, "key3"), "value3") == 0);
		
		ASSERT("T003 LOOKUP NONEXISTENT STRING",
				map_lookup(&m, "key1") == NULL);
		
		map_remove(&m, "key4");
		
		ASSERT("T004 REMOVE AND LOOKUP STRING",
				map_lookup(&m, "key4") == NULL);
		
		ASSERT("T006 LOOKUP STRING AFTER REMOVE 0",
				strcmp(map_lookup(&m, "key0"), "value0") == 0);
		ASSERT("T007 LOOKUP STRING AFTER REMOVE 1",
				strcmp(map_lookup(&m, "key3"), "value3") == 0);
		
		ASSERT("T008 REMOVE NONEXISTENT STRING",
				map_remove(&m, "key4") == NULL);
		
		map_free(&m);
	}
	
	{
		map_initial_capacity = 4;
		map m = map_init(NULL);
		
		map_insert(&m, "key0", "value0");
		map_insert(&m, "key3", "value3");
		map_insert(&m, "key4", "value4");
		
		ASSERT("T100 INSERT AND LOOKUP STRING 0",
				strcmp(map_lookup(&m, "key0"), "value0") == 0);
		ASSERT("T101 INSERT AND LOOKUP STRING 1",
				strcmp(map_lookup(&m, "key3"), "value3") == 0);
		ASSERT("T102 INSERT AND LOOKUP STRING 2",
				strcmp(map_lookup(&m, "key4"), "value4") == 0);
		
		ASSERT("T103 LOOKUP NONEXISTENT STRING",
				map_lookup(&m, "key1") == NULL);
		
		map_remove(&m, "key4");
		
		ASSERT("T104 REMOVE AND LOOKUP STRING",
				map_lookup(&m, "key4") == NULL);
		
		ASSERT("T106 LOOKUP STRING AFTER REMOVE 0",
				strcmp(map_lookup(&m, "key0"), "value0") == 0);
		ASSERT("T107 LOOKUP STRING AFTER REMOVE 1",
				strcmp(map_lookup(&m, "key3"), "value3") == 0);
		
		ASSERT("T108 REMOVE NONEXISTENT STRING",
				map_remove(&m, "key4") == NULL);
		
		map_free(&m);
	}
	
	printf("%d of %d tests passed\n", testspassed, testsrun);
	return testsfailed;
}