summaryrefslogtreecommitdiff
path: root/test/gettimeofday.c
blob: 3e6d33602a2f69a3f81f783b350fdadf2ff175a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "common.h"
#include "gettimeofday.h"
#include "mock/timer_mock.h"
#include "test_util.h"

#include <stddef.h>

static int test_gettimeofday_zero(void)
{
	struct timeval tv;
	timestamp_t now;

	now.val = 0;
	set_time(now);

	TEST_EQ(ec_gettimeofday(&tv, NULL), EC_SUCCESS, "%d");
	TEST_EQ(tv.tv_sec, 0L, "%ld");
	TEST_EQ(tv.tv_usec, 0L, "%ld");

	return EC_SUCCESS;
}

static int test_gettimeofday_zero_seconds(void)
{
	struct timeval tv;
	timestamp_t now;

	now.val = 100;
	set_time(now);

	TEST_EQ(ec_gettimeofday(&tv, NULL), EC_SUCCESS, "%d");
	TEST_EQ(tv.tv_sec, 0L, "%ld");
	TEST_EQ(tv.tv_usec, 100L, "%ld");

	return EC_SUCCESS;
}

static int test_gettimeofday_nonzero_seconds(void)
{
	struct timeval tv;
	timestamp_t now;

	now.val = 1000001;
	set_time(now);

	TEST_EQ(ec_gettimeofday(&tv, NULL), EC_SUCCESS, "%d");
	TEST_EQ(tv.tv_sec, 1L, "%ld");
	TEST_EQ(tv.tv_usec, 1L, "%ld");

	return EC_SUCCESS;
}

static int test_gettimeofday_max(void)
{
	struct timeval tv;
	timestamp_t now;

	now.val = UINT64_MAX;
	set_time(now);

	TEST_EQ(ec_gettimeofday(&tv, NULL), EC_SUCCESS, "%d");
	TEST_EQ(tv.tv_sec, 18446744073709L, "%ld");
	TEST_EQ(tv.tv_usec, 551615L, "%ld");

	return EC_SUCCESS;
}

static int test_gettimeofday_null_arg(void)
{
	TEST_EQ(ec_gettimeofday(NULL, NULL), EC_ERROR_INVAL, "%d");
	return EC_SUCCESS;
}

void run_test(int argc, const char **argv)
{
	/*
	 * Right now these tests only work on the host since they use a mock
	 * timer. Using a mock timer on device prevents the device from booting.
	 */
	test_reset();
	RUN_TEST(test_gettimeofday_zero);
	RUN_TEST(test_gettimeofday_zero_seconds);
	RUN_TEST(test_gettimeofday_nonzero_seconds);
	RUN_TEST(test_gettimeofday_max);
	RUN_TEST(test_gettimeofday_null_arg);
	test_print_result();
}