vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

map-test.c

(3895B)


      1 #include <stddef.h>
      2 #include <stdbool.h>
      3 #include <string.h>
      4 #include <stdio.h>
      5 #include <errno.h>
      6 #include "tap.h"
      7 #include "map.h"
      8 
      9 static bool get(Map *map, const char *key, const void *data) {
     10 	return map_get(map, key) == data && map_closest(map, key) == data;
     11 }
     12 
     13 static bool compare(const char *key, void *value, void *data) {
     14 	Map *map = data;
     15 	ok(map_get(map, key) == value, "Compare map content");
     16 	return true;
     17 }
     18 
     19 static bool once(const char *key, void *value, void *data) {
     20 	int *counter = data;
     21 	(*counter)++;
     22 	return false;
     23 }
     24 
     25 static bool visit(const char *key, void *value, void *data) {
     26 	int *index = value;
     27 	int *visited = data;
     28 	visited[*index]++;
     29 	return true;
     30 }
     31 
     32 static int order_counter;
     33 
     34 static bool order(const char *key, void *value, void *data) {
     35 	int *index = value;
     36 	int *order = data;
     37 	order[*index] = ++order_counter;
     38 	return true;
     39 }
     40 
     41 int main(int argc, char *argv[]) {
     42 	const char *key = "404";
     43 	const int values[3] = { 0, 1, 2 };
     44 
     45 	plan_no_plan();
     46 
     47 	Map *map = map_new();
     48 
     49 	ok(map && map_empty(map), "Creation");
     50 	ok(map_first(map, &key) == NULL && strcmp(key, "404") == 0, "First on empty map");
     51 	ok(map_empty(map_prefix(map, "404")), "Empty prefix map");
     52 
     53 	ok(!map_get(map, "404"), "Get non-existing key");
     54 	ok(!map_contains(map, "404"), "Contains non-existing key");
     55 	ok(!map_closest(map, "404") && errno == ENOENT, "Closest non-existing key");
     56 
     57 	ok(!map_put(map, "a", NULL) && errno == EINVAL && map_empty(map) && !map_get(map, "a"), "Put NULL value");
     58 	ok(map_put(map, "a", &values[0]) && !map_empty(map) && get(map, "a", &values[0]), "Put 1");
     59 	ok(map_first(map, &key) == &values[0] && strcmp(key, "a") == 0, "First on map with 1 value");
     60 	key = NULL;
     61 	ok(map_first(map_prefix(map, "a"), &key) == &values[0] && strcmp(key, "a") == 0, "First on prefix map");
     62 	ok(map_contains(map, "a"), "Contains existing key");
     63 	ok(map_closest(map, "a") == &values[0], "Closest match existing key");
     64 	ok(!map_put(map, "a", &values[1]) && errno == EEXIST && get(map, "a", &values[0]), "Put duplicate");
     65 	ok(map_put(map, "cafebabe", &values[2]) && get(map, "cafebabe", &values[2]), "Put 2");
     66 	ok(map_put(map, "cafe", &values[1]) && get(map, "cafe", &values[1]), "Put 3");
     67 	key = NULL;
     68 	ok(map_first(map_prefix(map, "cafe"), &key) == &values[1] && strcmp(key, "cafe") == 0, "First on prefix map with multiple suffixes");
     69 
     70 	Map *copy = map_new();
     71 	ok(map_copy(copy, map), "Copy");
     72 	ok(!map_empty(copy), "Not empty after copying");
     73 	map_iterate(copy, compare, map);
     74 	map_iterate(map, compare, copy);
     75 
     76 	int counter = 0;
     77 	map_iterate(copy, once, &counter);
     78 	ok(counter == 1, "Iterate stop condition");
     79 
     80 	ok(!map_get(map, "ca") && !map_closest(map, "ca") && errno == 0, "Closest ambigious");
     81 
     82 	int visited[] = { 0, 0, 0 };
     83 
     84 	map_iterate(map, visit, &visited);
     85 	ok(visited[0] == 1 && visited[1] == 1 && visited[2] == 1, "Iterate map");
     86 
     87 	memset(visited, 0, sizeof visited);
     88 	order_counter = 0;
     89 	map_iterate(map, order, &visited);
     90 	ok(visited[0] == 1 && visited[1] == 2 && visited[2] == 3, "Ordered iteration");
     91 
     92 	memset(visited, 0, sizeof visited);
     93 	map_iterate(map_prefix(map, "ca"), visit, &visited);
     94 	ok(visited[0] == 0 && visited[1] == 1 && visited[2] == 1, "Iterate sub map");
     95 
     96 	memset(visited, 0, sizeof visited);
     97 	order_counter = 0;
     98 	map_iterate(map_prefix(map, "ca"), order, &visited);
     99 	ok(visited[0] == 0 && visited[1] == 1 && visited[2] == 2, "Ordered sub map iteration");
    100 
    101 	ok(map_empty(map_prefix(map, "404")), "Empty map for non-existing prefix");
    102 
    103 	ok(!map_delete(map, "404"), "Delete non-existing key");
    104 	ok(map_delete(map, "cafe") == &values[1] && !map_get(map, "cafe"), "Delete existing key");
    105 	ok(map_closest(map, "cafe") == &values[2], "Closest unambigious");
    106 	ok(map_put(map, "cafe", &values[1]) && get(map, "cafe", &values[1]), "Put 3 again");
    107 
    108 	map_clear(map);
    109 	ok(map_empty(map), "Empty after clean");
    110 
    111 	map_free(map);
    112 	map_free(copy);
    113 
    114 	return exit_status();
    115 }