mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-06 16:56:42 +08:00
240 lines
4.6 KiB
C
240 lines
4.6 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/time.h>
|
|
#include <unistd.h>
|
|
#if defined(TEST_TARGET_unitt)
|
|
#include <varch/command.h>
|
|
#include <varch/unitt.h>
|
|
#else
|
|
#include "init.h"
|
|
#include "command.h"
|
|
#include "unitt.h"
|
|
#include "kern.h"
|
|
#endif
|
|
|
|
#if 0
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#include <psapi.h> // -lPsapi
|
|
#else
|
|
#include <unistd.h>
|
|
#include <pthread.h>
|
|
#endif
|
|
|
|
void get_memory_usage(int* resident_set_size, int* virtual_memory_size)
|
|
{
|
|
#ifdef _WIN32
|
|
PROCESS_MEMORY_COUNTERS pmc;
|
|
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
|
|
*resident_set_size = pmc.WorkingSetSize / 1024; // Converting to KB
|
|
*virtual_memory_size = pmc.PagefileUsage / 1024; // Converting to KB
|
|
} else {
|
|
*resident_set_size = 0;
|
|
*virtual_memory_size = 0;
|
|
}
|
|
#else
|
|
FILE* file = fopen("/proc/self/statm", "r");
|
|
if (file) {
|
|
fscanf(file, "%d %d", resident_set_size, virtual_memory_size);
|
|
fclose(file);
|
|
} else {
|
|
*resident_set_size = 0;
|
|
*virtual_memory_size = 0;
|
|
}
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
#define SINGLE_TCOUNT 1000
|
|
|
|
uint64_t unitt_clock(void)
|
|
{
|
|
struct timeval mstime;
|
|
uint64_t us = 0;
|
|
gettimeofday(&mstime, NULL);
|
|
us = mstime.tv_sec * 1000000 + mstime.tv_usec;
|
|
return us;
|
|
}
|
|
|
|
int unitt_add(int a, int b)
|
|
{
|
|
return a + b;
|
|
}
|
|
|
|
int unitt_subtract(int a, int b)
|
|
{
|
|
return a - b;
|
|
}
|
|
|
|
int unitt_multiply(int a, int b)
|
|
{
|
|
return a * b;
|
|
}
|
|
|
|
float unitt_divide(int a, int b)
|
|
{
|
|
if (b == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
return (float)a / (float)b;
|
|
}
|
|
|
|
int setup()
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int teardown()
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int random_input()
|
|
{
|
|
return rand() % 100;
|
|
}
|
|
|
|
int test_add()
|
|
{
|
|
for (int i = 0; i < SINGLE_TCOUNT; i++)
|
|
{
|
|
int a = random_input();
|
|
int b = random_input();
|
|
if (UNITT_E_FAIL == unitt_det_equal(unitt_add(a, b), a + b, "Random addition failed")) return UNITT_E_FAIL;
|
|
}
|
|
|
|
return UNITT_E_OK;
|
|
}
|
|
|
|
int test_subtract()
|
|
{
|
|
for (int i = 0; i < SINGLE_TCOUNT; i++)
|
|
{
|
|
int a = random_input();
|
|
int b = random_input();
|
|
if (UNITT_E_FAIL == unitt_det_equal(unitt_subtract(a, b), a - b, "Random subtraction failed")) return UNITT_E_FAIL;
|
|
}
|
|
|
|
return UNITT_E_OK;
|
|
}
|
|
|
|
int test_multiply()
|
|
{
|
|
for (int i = 0; i < SINGLE_TCOUNT; i++)
|
|
{
|
|
int a = random_input();
|
|
int b = random_input();
|
|
if (UNITT_E_FAIL == unitt_det_equal(unitt_multiply(a, b), a * b, "Random multiplication failed")) return UNITT_E_FAIL;
|
|
}
|
|
|
|
return UNITT_E_OK;
|
|
}
|
|
|
|
int test_divide()
|
|
{
|
|
for (int i = 0; i < SINGLE_TCOUNT; i++)
|
|
{
|
|
int a = random_input();
|
|
int b = random_input();
|
|
if (b != 0)
|
|
{
|
|
if (UNITT_E_FAIL == unitt_det_float(unitt_divide(a, b), (float)a / (float)b, 0.00001, "Random division failed")) return UNITT_E_FAIL;
|
|
}
|
|
}
|
|
|
|
return UNITT_E_OK;
|
|
}
|
|
|
|
void unitt_main()
|
|
{
|
|
static UNITT_TCASE math_tests[] = {
|
|
UNITT_TCASE(test_add),
|
|
UNITT_TCASE(test_subtract),
|
|
UNITT_TCASE(test_multiply),
|
|
UNITT_TCASE(test_divide),
|
|
};
|
|
|
|
static UNITT suites[] = {
|
|
{ "Math Suite", math_tests, sizeof(math_tests) / sizeof(math_tests[0]) , unitt_clock },
|
|
};
|
|
|
|
UNITT_EXE(suites);
|
|
}
|
|
|
|
static void usage(void)
|
|
{
|
|
printf(
|
|
"Usage: unitt [opt] [arg]\n"
|
|
"\n"
|
|
"options:\n"
|
|
" -p <period> Test period, unit ms\n"
|
|
" -h Print help\n"
|
|
" -v Print version\n"
|
|
"\n"
|
|
);
|
|
}
|
|
|
|
static int test(int argc, char *argv[])
|
|
{
|
|
int period = 1000; // ms
|
|
|
|
/* reset getopt */
|
|
command_opt_init();
|
|
|
|
while (1)
|
|
{
|
|
int opt = command_getopt(argc, argv, "p:hv");
|
|
if (opt == -1) break;
|
|
|
|
switch (opt)
|
|
{
|
|
case 'p' :
|
|
period = atoi(command_optarg);
|
|
break;
|
|
case 'v' :
|
|
printf("unitt version %d.%d.%d\r\n", UNITT_V_MAJOR, UNITT_V_MINOR, UNITT_V_PATCH);
|
|
return 0;
|
|
case '?':
|
|
printf("Unknown option `%c`\r\n", command_optopt);
|
|
return -1;
|
|
case 'h' :
|
|
default:
|
|
usage();
|
|
return 0;
|
|
}
|
|
}
|
|
if (1 == command_optind) // no opt
|
|
{
|
|
unitt_main();
|
|
return 0;
|
|
}
|
|
|
|
srand((unsigned int)time(NULL));
|
|
|
|
#if defined(TEST_TARGET_unitt)
|
|
while (1)
|
|
{
|
|
unitt_main();
|
|
usleep(1000 * period);
|
|
}
|
|
#else
|
|
printf("create task %d\r\n", task_create(period, unitt_main));
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
|
|
#if defined(TEST_TARGET_unitt)
|
|
int main(int argc, char *argv[])
|
|
{
|
|
return test(argc, argv);
|
|
}
|
|
#else
|
|
void test_unitt(void)
|
|
{
|
|
command_export("unitt", test);
|
|
}
|
|
init_export_app(test_unitt);
|
|
#endif
|