File: assert-test.c

package info (click to toggle)
weston 14.0.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,952 kB
  • sloc: ansic: 140,007; xml: 2,480; python: 106; makefile: 34; sh: 33
file content (153 lines) | stat: -rw-r--r-- 4,174 bytes parent folder | download | duplicates (3)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright 2022 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <stdlib.h>
#include <stdarg.h>

#define custom_assert_fail_ test_assert_report

#include "shared/weston-assert.h"
#include "weston-test-runner.h"

__attribute__((format(printf, 2, 3)))
static void
test_assert_report(const struct weston_compositor *compositor, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
}

static void
abort_if_not(bool cond)
{
	if (!cond)
		abort();
}

struct my_type {
	int x;
	float y;
};

/* Demonstration of custom type comparison */
static int
my_type_cmp(const struct my_type *a, const struct my_type *b)
{
	if (a->x < b->x)
		return -1;
	if (a->x > b->x)
		return 1;
	if (a->y < b->y)
		return -1;
	if (a->y > b->y)
		return 1;
	return 0;
}

#define weston_assert_my_type_lt(compositor, a, b) \
	weston_assert_fn_(compositor, my_type_cmp, a, b, const struct my_type *, "my_type %p", <)

TEST(asserts)
{
	/* Unused by the macros for now, so let's just use NULL. */
	struct weston_compositor *compositor = NULL;
	bool ret;

	ret = weston_assert_true(compositor, false);
	abort_if_not(ret == false);

	ret = weston_assert_true(compositor, true);
	abort_if_not(ret);

	ret = weston_assert_false(compositor, true);
	abort_if_not(ret == false);

	ret = weston_assert_false(compositor, false);
	abort_if_not(ret);

	ret = weston_assert_true(compositor, true && false);
	abort_if_not(ret == false);

	ret = weston_assert_ptr(compositor, &ret);
	abort_if_not(ret);

	ret = weston_assert_ptr(compositor, NULL);
	abort_if_not(ret == false);

	ret = weston_assert_ptr_is_null(compositor, NULL);
	abort_if_not(ret);

	ret = weston_assert_ptr_is_null(compositor, &ret);
	abort_if_not(ret == false);

	ret = weston_assert_ptr_eq(compositor, &ret, &ret);
	abort_if_not(ret);

	ret = weston_assert_ptr_eq(compositor, &ret, &ret + 1);
	abort_if_not(ret == false);

	double fifteen = 15.0;
	ret = weston_assert_double_eq(compositor, fifteen, 15.000001);
	abort_if_not(ret == false);

	ret = weston_assert_double_eq(compositor, fifteen, 15);
	abort_if_not(ret);

	const char *nom = "bar";
	ret = weston_assert_str_eq(compositor, nom, "bar");
	abort_if_not(ret);
	ret = weston_assert_str_eq(compositor, nom, "baz");
	abort_if_not(ret == false);

	struct my_type a = { 1, 2.0 };
	struct my_type b = { 0, 2.0 };
	ret = weston_assert_my_type_lt(compositor, &b, &a);
	abort_if_not(ret);
	ret = weston_assert_my_type_lt(compositor, &a, &b);
	abort_if_not(ret == false);

	uint32_t bitfield = 0xffff;
	ret = weston_assert_bit_is_set(compositor, bitfield, 2);
	abort_if_not(ret);
	ret = weston_assert_bit_is_set(compositor, bitfield, 57);
	abort_if_not(ret == false);

	uint64_t max_uint64 = UINT64_MAX;
	ret = weston_assert_uint64_eq(compositor, max_uint64, 0);
	abort_if_not(ret == false);

	uint64_t val = 0x200010001000ffff;
	uint64_t msk = 0x00000000fffffff3;
	ret = weston_assert_legal_bits(compositor, val, msk);
	abort_if_not(ret == false);

	ret = weston_assert_legal_bits(compositor, val, UINT64_MAX);
	abort_if_not(ret);
}