test_timer.c 2.2 KB
Newer Older
M
m00302376 已提交
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
 */

/*
 * Tests for include/tst_timer.h
 */

#include "tst_test.h"
#include "tst_timer.h"

#define VAL_MS 1001
#define VAL_US 1001000

static void test_diff(enum tst_ts_type type)
{
	struct tst_ts ts1, ts2;
	long long diff;

	ts1 = tst_ts_from_ms(type, VAL_MS);
	ts2 = tst_ts_from_us(type, VAL_US);

	diff = tst_ts_diff_ns(ts1, ts2);

	if (diff == 0)
		tst_res(TPASS, "ns_diff = 0");
	else
		tst_res(TFAIL, "ns_diff = %lli", diff);

	diff = tst_ts_diff_ns(ts1, ts2);

	if (diff == 0)
		tst_res(TPASS, "us_diff = 0");
	else
		tst_res(TFAIL, "us_diff = %lli", diff);

	diff = tst_ts_diff_ms(ts1, ts2);

	if (diff == 0)
		tst_res(TPASS, "ms_diff = 0");
	else
		tst_res(TFAIL, "ms_diff = %lli", diff);
}

static void test_lt(enum tst_ts_type type)
{
	struct tst_ts ts1, ts2;

	ts1 = tst_ts_from_ms(type, VAL_MS);
	ts2 = tst_ts_from_us(type, VAL_US + 1);

	if (tst_ts_lt(ts1, ts2))
		tst_res(TPASS, "ts1 < ts2");
	else
		tst_res(TFAIL, "ts1 >= ts2");

	ts1 = tst_ts_add_us(ts1, 1);

	if (tst_ts_lt(ts1, ts2))
		tst_res(TFAIL, "ts1 < ts2");
	else
		tst_res(TPASS, "ts1 >= ts2");

	ts1 = tst_ts_add_us(ts1, 1);

	if (tst_ts_lt(ts1, ts2))
		tst_res(TFAIL, "ts1 < ts2");
	else
		tst_res(TPASS, "ts1 >= ts2");
}

static void test_add_sub(enum tst_ts_type type)
{
	struct tst_ts ts;

	ts = tst_ts_from_ns(type, 999999000);
	ts = tst_ts_add_us(ts, 1);

	long long sec = tst_ts_get_sec(ts);
	long long nsec = tst_ts_get_nsec(ts);

	/* Check that result was normalized */
	if (sec != 1 || nsec != 0)
		tst_res(TFAIL, "sec = %lli, nsec = %lli", sec, nsec);
	else
		tst_res(TPASS, "sec = %lli, nsec = %lli", sec, nsec);

	ts = tst_ts_from_ms(type, 1000);
	ts = tst_ts_sub_us(ts, 1);

	sec = tst_ts_get_sec(ts);
	nsec = tst_ts_get_nsec(ts);

	/* Check that result was normalized */
	if (sec != 0 || nsec != 999999000)
		tst_res(TFAIL, "sec = %lli, nsec = %lli", sec, nsec);
	else
		tst_res(TPASS, "sec = %lli, nsec = %lli", sec, nsec);
}

static void do_test(unsigned int n)
{
	tst_res(TINFO, "Testing with type = %i", n);
	test_diff(n);
	test_lt(n);
	test_add_sub(n);
}

static struct tst_test test = {
	.test = do_test,
	.tcnt = 3,
};