Skip to content

Commit 09e70b9

Browse files
committed
Fix: default random seed might exceed the limit
1 parent 39dbd03 commit 09e70b9

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## v3.0.4
44

5+
### Fixed
6+
1. Fix: default random seed might exceed the limit.
7+
58
### Features
69
1. Automatic disable thread support if `Threads` not found.
710
2. Test case can be unregistered by `cutest_unregister_case()`.

include/cutest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ extern "C" {
115115
/**
116116
* @brief Development version.
117117
*/
118-
#define CUTEST_VERSION_PREREL 2
118+
#define CUTEST_VERSION_PREREL 3
119119

120120
/**
121121
* @brief Ensure the api is exposed as C function.

src/cutest.c

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ static void cutest_porting_srand(unsigned long s)
5252
s_test_rand_seed = s;
5353
}
5454

55+
static unsigned long cutest_porting_grand(void)
56+
{
57+
return s_test_rand_seed;
58+
}
59+
5560
static unsigned long cutest_porting_rand(void)
5661
{
5762
s_test_rand_seed = 1103515245UL * s_test_rand_seed + 12345;
@@ -1784,8 +1789,6 @@ typedef struct test_ctx
17841789
unsigned shuffle : 1; /**< Randomize running cases */
17851790
} mask;
17861791

1787-
unsigned long shuffle_seed;
1788-
17891792
struct
17901793
{
17911794
cutest_porting_jmpbuf_t* addr; /**< Jump address. */
@@ -1847,7 +1850,6 @@ static test_ctx_t g_test_ctx = {
18471850
{ { 0, 0, 0, 0, 0 }, { 0, 0 } }, /* .counter */
18481851
{ { NULL, 0 } }, /* .filter */
18491852
{ 0, 0, 0, 0 }, /* .mask */
1850-
0, /* .shuffle_seed */
18511853
{ NULL, NULL }, /* .jmp */
18521854
NULL, /* .out */
18531855
NULL, /* .hook */
@@ -2757,8 +2759,6 @@ static int _cutest_setup_arg_print_time(const char* str)
27572759
static void _cutest_srand(unsigned long s)
27582760
{
27592761
s = s % (MAX_RAND + 1);
2760-
2761-
g_test_ctx.shuffle_seed = s;
27622762
cutest_porting_srand(s);
27632763
}
27642764

@@ -3081,15 +3081,14 @@ static void _cutest_setup_once(void)
30813081
{
30823082
token = 1;
30833083
_cutest_setup_type();
3084-
_cutest_srand(1);
30853084
}
30863085
}
30873086

30883087
static void _cutest_prepare(void)
30893088
{
30903089
cutest_porting_timespec_t seed;
30913090
cutest_porting_clock_gettime(&seed);
3092-
cutest_porting_srand((unsigned long)seed.tv_sec);
3091+
_cutest_srand((unsigned long)seed.tv_sec);
30933092

30943093
g_test_ctx.runtime.tid = cutest_porting_gettid();
30953094
g_test_ctx.counter.repeat.repeat = 1;
@@ -3131,6 +3130,15 @@ static int _cutest_setup_arg_break_on_failure(void)
31313130
return 0;
31323131
}
31333132

3133+
static void _cutest_cleanup(void)
3134+
{
3135+
cutest_map_t case_table = g_test_ctx.case_table;
3136+
cutest_map_t type_table = g_test_ctx.type_table;
3137+
cutest_porting_memset(&g_test_ctx, 0, sizeof(g_test_ctx));
3138+
g_test_ctx.case_table = case_table;
3139+
g_test_ctx.type_table = type_table;
3140+
}
3141+
31343142
/**
31353143
* @brief Setup test context
31363144
* @param[in] argc The number of command line argument.
@@ -3170,16 +3178,9 @@ static int _cutest_setup(int argc, char* argv[], FILE* out, const cutest_hook_t*
31703178
continue;\
31713179
} while (0)
31723180

3173-
/* Do soft clear. */
3174-
{
3175-
cutest_map_t case_table = g_test_ctx.case_table;
3176-
cutest_map_t type_table = g_test_ctx.type_table;
3177-
cutest_porting_memset(&g_test_ctx, 0, sizeof(g_test_ctx));
3178-
g_test_ctx.case_table = case_table;
3179-
g_test_ctx.type_table = type_table;
3180-
}
3181-
31823181
_cutest_setup_once();
3182+
3183+
_cutest_cleanup();
31833184
_cutest_prepare();
31843185

31853186
g_test_ctx.out = out;
@@ -3233,16 +3234,6 @@ static void _cutest_run_all_test_once(void)
32333234
_cutest_show_report(&tv_total_start, &tv_total_end);
32343235
}
32353236

3236-
static void _cutest_cleanup(void)
3237-
{
3238-
cutest_porting_memset(&g_test_ctx.runtime, 0, sizeof(g_test_ctx.runtime));
3239-
cutest_porting_memset(&g_test_ctx.counter, 0, sizeof(g_test_ctx.counter));
3240-
cutest_porting_memset(&g_test_ctx.mask, 0, sizeof(g_test_ctx.mask));
3241-
cutest_porting_memset(&g_test_ctx.filter, 0, sizeof(g_test_ctx.filter));
3242-
3243-
g_test_ctx.hook = NULL;
3244-
}
3245-
32463237
static void _cutest_show_information(void)
32473238
{
32483239
#if CUTEST_VERSION_PREREL
@@ -3255,7 +3246,7 @@ static void _cutest_show_information(void)
32553246
cutest_porting_fprintf(g_test_ctx.out,
32563247
"[ $PARAME. ] --test_shuffle=%d\n", (int)g_test_ctx.mask.shuffle);
32573248
cutest_porting_fprintf(g_test_ctx.out,
3258-
"[ $PARAME. ] --test_random_seed=%lu\n", g_test_ctx.shuffle_seed);
3249+
"[ $PARAME. ] --test_random_seed=%lu\n", cutest_porting_grand());
32593250
cutest_porting_fprintf(g_test_ctx.out,
32603251
"[ $PARAME. ] --test_also_run_disabled_tests=%d\n", (int)g_test_ctx.mask.also_run_disabled_tests);
32613252
cutest_porting_fprintf(g_test_ctx.out,

0 commit comments

Comments
 (0)