xref: /xnu-11417.121.6/tests/gettimeofday.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributions #include <unistd.h>
2*a1e26a70SApple OSS Distributions #include <sys/time.h>
3*a1e26a70SApple OSS Distributions #include <mach/mach_time.h>
4*a1e26a70SApple OSS Distributions 
5*a1e26a70SApple OSS Distributions #include <darwintest.h>
6*a1e26a70SApple OSS Distributions 
7*a1e26a70SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
8*a1e26a70SApple OSS Distributions 
9*a1e26a70SApple OSS Distributions extern int __gettimeofday(struct timeval *, struct timezone *);
10*a1e26a70SApple OSS Distributions 
11*a1e26a70SApple OSS Distributions T_DECL(gettimeofday, "gettimeofday()",
12*a1e26a70SApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(true), T_META_LTEPHASE(LTE_POSTINIT))
13*a1e26a70SApple OSS Distributions {
14*a1e26a70SApple OSS Distributions 	struct timeval tv_a, tv_b, tv_c;
15*a1e26a70SApple OSS Distributions 
16*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(gettimeofday(&tv_a, NULL), NULL);
17*a1e26a70SApple OSS Distributions 	T_ASSERT_GT(tv_a.tv_sec, 0L, NULL);
18*a1e26a70SApple OSS Distributions 
19*a1e26a70SApple OSS Distributions 	sleep(1);
20*a1e26a70SApple OSS Distributions 
21*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(__gettimeofday(&tv_b, NULL), NULL);
22*a1e26a70SApple OSS Distributions 	T_ASSERT_GE(tv_b.tv_sec, tv_a.tv_sec, NULL);
23*a1e26a70SApple OSS Distributions 
24*a1e26a70SApple OSS Distributions 	sleep(1);
25*a1e26a70SApple OSS Distributions 
26*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(gettimeofday(&tv_c, NULL), NULL);
27*a1e26a70SApple OSS Distributions 	T_ASSERT_GE(tv_c.tv_sec, tv_b.tv_sec, NULL);
28*a1e26a70SApple OSS Distributions }
29*a1e26a70SApple OSS Distributions 
30*a1e26a70SApple OSS Distributions #if 0 // This symbol isn't exported so we can't test with stock libsyscall
31*a1e26a70SApple OSS Distributions extern int __gettimeofday_with_mach(struct timeval *, struct timezone *, uint64_t *mach_time);
32*a1e26a70SApple OSS Distributions 
33*a1e26a70SApple OSS Distributions T_DECL(gettimeofday_with_mach, "gettimeofday_with_mach()",
34*a1e26a70SApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(true))
35*a1e26a70SApple OSS Distributions {
36*a1e26a70SApple OSS Distributions 	struct timeval gtod_ts;
37*a1e26a70SApple OSS Distributions 
38*a1e26a70SApple OSS Distributions 	uint64_t mach_time_before, mach_time, mach_time_after;
39*a1e26a70SApple OSS Distributions 
40*a1e26a70SApple OSS Distributions 	mach_time_before = mach_absolute_time();
41*a1e26a70SApple OSS Distributions 
42*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(__gettimeofday_with_mach(&gtod_ts, NULL, &mach_time), NULL);
43*a1e26a70SApple OSS Distributions 	T_ASSERT_GT(gtod_ts.tv_sec, 0L, NULL);
44*a1e26a70SApple OSS Distributions 
45*a1e26a70SApple OSS Distributions 	mach_time_after = mach_absolute_time();
46*a1e26a70SApple OSS Distributions 
47*a1e26a70SApple OSS Distributions 	T_LOG("%llx > %llx > %llx", mach_time_before, mach_time, mach_time_after);
48*a1e26a70SApple OSS Distributions 
49*a1e26a70SApple OSS Distributions 	T_ASSERT_LT(mach_time_before, mach_time, NULL);
50*a1e26a70SApple OSS Distributions 	T_ASSERT_GT(mach_time_after, mach_time, NULL);
51*a1e26a70SApple OSS Distributions }
52*a1e26a70SApple OSS Distributions #endif // 0
53