xref: /reactos/modules/rostests/apitests/crt/rand_s.c (revision d6eebaa4)
1 /*
2  * PROJECT:     ReactOS API Tests
3  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
4  * PURPOSE:     Tests for rand_s
5  * COPYRIGHT:   Copyright 2024 Timo Kreuzer <timo.kreuzer@reactos.org>
6  */
7 
8 #include <apitest.h>
9 #include <stdio.h>
10 
11 #ifdef TEST_STATIC_CRT
12 errno_t __cdecl rand_s(_Out_ unsigned int* _RandomValue);
13 #endif
14 
15 typedef int __cdecl rand_s_t(unsigned int*);
16 rand_s_t *p_rand_s;
17 
18 void test_rand_s_performance(void)
19 {
20     unsigned long long start, end;
21     unsigned int val;
22     int i;
23 
24     start = __rdtsc();
25     for (i = 0; i < 10000; i++)
26     {
27         p_rand_s(&val);
28     }
29     end = __rdtsc();
30     printf("rand_s took %llu cycles\n", end - start);
31 }
32 
33 START_TEST(rand_s)
34 {
35     unsigned int val;
36     int ret;
37 
38 #ifndef TEST_STATIC_CRT
39     /* Dynamically load rand_s from mvcrt */
40     HMODULE msvcrt = GetModuleHandleA("msvcrt");
41     p_rand_s = (rand_s_t*)GetProcAddress(msvcrt, "rand_s");
42     if (!p_rand_s)
43     {
44         win_skip("rand_s is not available\n");
45         return;
46     }
47 #else
48     p_rand_s = rand_s;
49 #endif
50 
51     /* Test performance */
52     test_rand_s_performance();
53 
54     /* Test with NULL pointer */
55     ret = p_rand_s(NULL);
56     ok(ret == EINVAL, "Expected EINVAL, got %d\n", ret);
57 
58     /* Test with valid pointer */
59     ret = p_rand_s(&val);
60     ok(ret == 0, "Expected 0, got %d\n", ret);
61 }
62