mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-07 17:26:43 +08:00
130 lines
2.8 KiB
C
130 lines
2.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#if defined(TEST_TARGET_map)
|
|
#include <varch/command.h>
|
|
#include <varch/map.h>
|
|
#else
|
|
#include "init.h"
|
|
#include "command.h"
|
|
#include "map.h"
|
|
#endif
|
|
|
|
static void test_it(void)
|
|
{
|
|
map_t map = map(string, int);
|
|
int value;
|
|
char *key;
|
|
void *data;
|
|
int i;
|
|
|
|
value = 100; map_insert(map, mpair("hello", &value));
|
|
value = 1; map_insert(map, mpair("ZhangSan", &value));
|
|
value = 2; map_insert(map, mpair("LiSi", &value));
|
|
value = 3; map_insert(map, mpair("WangWu", &value));
|
|
value = 4; map_insert(map, mpair("SunLiu", &value));
|
|
value = 5; map_insert(map, mpair("QianQi", &value));
|
|
|
|
map_it_init(map, MAP_HEAD);
|
|
i = map_size(map);
|
|
while (i--)
|
|
{
|
|
data = map_it_get(map, &key, NULL);
|
|
printf("map[%s] = %d\r\n", key, *(int *)data);
|
|
}
|
|
|
|
_map(map);
|
|
}
|
|
|
|
static void test_base(void)
|
|
{
|
|
int value;
|
|
map_t map = map(string, int);
|
|
if (!map) return;
|
|
|
|
value = 100; map_insert(map, mpair("hello", &value));
|
|
value = 110; map_insert(map, mpair("Zhang", &value));
|
|
|
|
printf("size = %d, key size = %d, value size = %d\r\n", map_size(map), map_ksize(map), map_vsize(map));
|
|
|
|
printf("map[hello] = %d\r\n", map_at(map, int, "hello"));
|
|
printf("map[Zhang] = %d\r\n", map_at(map, int, "Zhang"));
|
|
map_erase(map, "hello");
|
|
|
|
_map(map);
|
|
}
|
|
|
|
static void test_float_map(void)
|
|
{
|
|
int value;
|
|
map_t map = map(double, int);
|
|
if (!map) return;
|
|
|
|
value = 111; map_insert(map, mpair(12.3, &value));
|
|
value = 100; map_insert(map, mpair(16.9, &value));
|
|
printf("%d\r\n", map_at(map, int, 16.9000000000000000000001));
|
|
map_erase(map, 16.9);
|
|
_map(map);
|
|
}
|
|
|
|
static void usage(void)
|
|
{
|
|
printf(
|
|
"Usage: map [opt] [arg]\n"
|
|
"\n"
|
|
"options:\n"
|
|
" -h Print help\n"
|
|
" -v Print version\n"
|
|
"\n"
|
|
"argument:\n"
|
|
" <read> Test default read function\n"
|
|
" <write> Test default write function\n"
|
|
);
|
|
}
|
|
|
|
static int test(int argc, char *argv[])
|
|
{
|
|
/* reset getopt */
|
|
command_opt_init();
|
|
|
|
while (1)
|
|
{
|
|
int opt = command_getopt(argc, argv, "hv");
|
|
if (opt == -1) break;
|
|
|
|
switch (opt)
|
|
{
|
|
case 'v' :
|
|
printf("map version %d.%d.%d\r\n", MAP_V_MAJOR, MAP_V_MINOR, MAP_V_PATCH);
|
|
return 0;
|
|
case '?':
|
|
printf("Unknown option `%c`\r\n", command_optopt);
|
|
return -1;
|
|
case 'h' :
|
|
default:
|
|
usage();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
test_it();
|
|
// test_base();
|
|
// test_float_map();
|
|
|
|
return 0;
|
|
}
|
|
|
|
#if defined(TEST_TARGET_map)
|
|
int main(int argc, char *argv[])
|
|
{
|
|
return test(argc, argv);
|
|
}
|
|
#else
|
|
void test_map(void)
|
|
{
|
|
command_export("map", test);
|
|
}
|
|
init_export_app(test_map);
|
|
#endif
|
|
|