forked from lei.chen/myxce
95 lines
2 KiB
C
95 lines
2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unity/unity.h>
|
|
|
|
#include "idgen.h"
|
|
#include "utils.h"
|
|
#include <errno.h>
|
|
|
|
static void test_idgen_invalid(void)
|
|
{
|
|
idgen_ctx_t ctx;
|
|
uint64_t idx;
|
|
|
|
// TEST_ASSERT_TRUE(0 == 1);
|
|
TEST_ASSERT_TRUE(idgen_ctx_init(NULL) == -EINVAL);
|
|
|
|
idgen_ctx_init(&ctx);
|
|
|
|
TEST_ASSERT_TRUE(idgen(&ctx, 128, 3, 1, &idx) == -EINVAL);
|
|
TEST_ASSERT_TRUE(idgen(&ctx, 32, 128, 1, &idx) == -EINVAL);
|
|
TEST_ASSERT_TRUE(idgen(&ctx, 32, 3, 0, &idx) == -EINVAL);
|
|
TEST_ASSERT_TRUE(idgen(&ctx, 32, 3, 65536, &idx) == -EINVAL);
|
|
TEST_ASSERT_TRUE(idgen(&ctx, 32, 3, 128, NULL) == -EINVAL);
|
|
|
|
idgen_ctx_fini(&ctx);
|
|
}
|
|
|
|
static void test_idgen1(void)
|
|
{
|
|
idgen_ctx_t ctx;
|
|
uint64_t idx;
|
|
int rc, i;
|
|
|
|
rc = idgen_ctx_init(&ctx);
|
|
TEST_ASSERT_TRUE(rc >= 0);
|
|
|
|
for (i = 0; i < 20; i++) {
|
|
char buf[256];
|
|
|
|
rc = idgen(&ctx, 0x0, 0x0, 1, &idx);
|
|
TEST_ASSERT_TRUE(rc >= 0);
|
|
memset(buf, 0x0, sizeof(buf));
|
|
fmt_jobid(buf, sizeof(buf), idx);
|
|
printf("\tjob = %s idx %lx\n", buf, idx);
|
|
}
|
|
|
|
idgen_ctx_fini(&ctx);
|
|
}
|
|
|
|
static void test_idgen_array(void)
|
|
{
|
|
idgen_ctx_t ctx;
|
|
uint64_t idx;
|
|
int rc, i;
|
|
|
|
rc = idgen_ctx_init(&ctx);
|
|
TEST_ASSERT_TRUE(rc >= 0);
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
int k;
|
|
|
|
for (k = 0; k < 5; k++) {
|
|
int j;
|
|
|
|
rc = idgen(&ctx, 0x0, 0x0, i + 1, &idx);
|
|
TEST_ASSERT_TRUE(rc >= 0);
|
|
if (i >= 1) {
|
|
TEST_ASSERT_TRUE((idx & MAX_ARRAY_IDX) == 0);
|
|
}
|
|
|
|
for (j = 0; j < i; j++) {
|
|
char buf[256];
|
|
|
|
memset(buf, 0x0, sizeof(buf));
|
|
|
|
fmt_jobid(buf, sizeof(buf), idx + j);
|
|
printf("\tjob = %s idx %lx \n", buf, idx + j);
|
|
}
|
|
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
idgen_ctx_fini(&ctx);
|
|
}
|
|
|
|
void test_idgen(void)
|
|
{
|
|
printf("\n>> idgen test\n\n");
|
|
|
|
RUN_TEST(test_idgen_invalid);
|
|
RUN_TEST(test_idgen1);
|
|
RUN_TEST(test_idgen_array);
|
|
}
|