varch/test/test_search.c
2024-11-16 13:36:54 +08:00

123 lines
4.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_search)
#include <varch/command.h>
#include <varch/search.h>
#else
#include "init.h"
#include "command.h"
#include "search.h"
#endif
static void* int_array_addr(void *array, int index)
{
return (void *)(&((int *)array)[index]);
}
static int int_cmp(void *element, void *target)
{
if (*(int *)element > *(int *)target) return 1;
else if (*(int *)element < *(int *)target) return -1;
return 0;
}
static void test_linear(void)
{
int array[] = {6, 3, 1, 0, -2, 9};
SearchOPS ops;
int target = 1;
ops.addr = int_array_addr;
ops.cmp = int_cmp;
target = 9, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = -2, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 0, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 1, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 3, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 6, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 5, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = -9, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
}
static void test_binary(void)
{
int array[] = {0, 3, 6, 10, 62, 99};
SearchOPS ops;
int target = 10;
ops.addr = int_array_addr;
ops.cmp = int_cmp;
target = 0, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 3, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 6, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 10, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 62, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 99, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = 5, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
target = -9, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops));
}
static void usage(void)
{
printf(
"Usage: search [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("search version %d.%d.%d\r\n", SEARCH_V_MAJOR, SEARCH_V_MINOR, SEARCH_V_PATCH);
return 0;
case '?':
printf("Unknown option `%c`\r\n", command_optopt);
return -1;
case 'h' :
default:
usage();
return 0;
}
}
// test_linear();
test_binary();
return 0;
}
#if defined(TEST_TARGET_search)
int main(int argc, char *argv[])
{
return test(argc, argv);
}
#else
void test_search(void)
{
command_export("search", test);
}
init_export_app(test_search);
#endif