aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/gl-2.0/api/clip-flag-behavior.c
blob: 0b635411b7edf11599257a5af696a682b857cf6c (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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright © 2011 Intel Corporation
 *
 * 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.
 */

/**
 * \file clip-flag-behavior.c
 *
 * This test verifies the following basic behaviors of the GL_CLIP_PLANEi
 * flags:
 *
 * - There are exactly MAX_CLIP_PLANES of them, and trying to access a
 *   nonexistent flag produces a GL_INVALID_ENUM error.
 *
 * - They default to false.
 *
 * - Their behavior under glGetBooleanv, glIsEnabled, glEnable, and
 *   glDisable is consistent.
 */

#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_MAIN(
    100 /*window_width*/,
    100 /*window_height*/,
    PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE)

enum piglit_result
piglit_display(void)
{
	return PIGLIT_FAIL;
}

static char *bool_to_string(bool b)
{
	if (b)
		return "true";
	else
		return "false";
}

static bool
check_error(GLenum expected)
{
	GLenum err = glGetError();
	if (err == expected) {
		return true;
	} else {
		printf("Expected '%s', got '%s'\n", gluErrorString(expected),
		       gluErrorString(err));
		return false;
	}
}

static bool
check_bool(GLboolean b, bool expected)
{
	/* Negate the bools before comparing so that distinct nonzero
	 * values compare equal.
	 */
	if ((!b) == (!expected)) {
		return true;
	} else {
		printf("Expected %s, got %s\n", bool_to_string(expected),
		       bool_to_string(b));
		return false;
	}
}

/**
 * Print "OK" and return true.  This is helpful in chaining with the
 * functions above, e.g.:
 *
 * bool pass = check_error(...) && check_bool(...) && print_ok();
 */
static bool
print_ok()
{
	printf("OK\n");
	return true;
}

static bool
check_enable_state(char *enum_name, GLenum enum_value, bool expected)
{
	GLboolean b;

	bool pass = true;
	printf("Trying glIsEnabled(%s): ", enum_name);
	b = glIsEnabled(enum_value);
	pass = (check_error(GL_NO_ERROR) && check_bool(b, expected)
		&& print_ok()) && pass;

	printf("Trying glGetBooleanv(%s): ", enum_name);
	glGetBooleanv(enum_value, &b);
	pass = (check_error(GL_NO_ERROR) && check_bool(b, expected)
		&& print_ok()) && pass;

	return pass;
}

void
piglit_init(int argc, char **argv)
{
	GLint max_clip_planes;
	bool pass = true;
	GLint plane;
	GLboolean b;
	char enum_name[38];
	GLenum enum_value;

	printf("Querying GL_MAX_CLIP_PLANES: ");
	glGetIntegerv(GL_MAX_CLIP_PLANES, &max_clip_planes);
	if (!check_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);
	printf("%d\n", max_clip_planes);
	if (max_clip_planes < 0) {
		printf("Error: GL_MAX_CLIP_PLANES must be >= 0\n");
		piglit_report_result(PIGLIT_FAIL);
	}

	/* Check behavior of GL_CLIP_PLANE0 + i where i < max_clip_planes */
	for (plane = 0; plane < max_clip_planes; ++plane) {
		enum_value = GL_CLIP_PLANE0 + plane;
		sprintf(enum_name, "GL_CLIP_PLANE0 + %d", plane);

		pass = check_enable_state(enum_name, enum_value, false) && pass;

		printf("Trying glEnable(%s): ", enum_name);
		glEnable(enum_value);
		pass = (check_error(GL_NO_ERROR) && print_ok()) && pass;

		pass = check_enable_state(enum_name, enum_value, true) && pass;

		printf("Trying glDisable(%s): ", enum_name);
		glDisable(enum_value);
		pass = (check_error(GL_NO_ERROR) && print_ok()) && pass;

		pass = check_enable_state(enum_name, enum_value, false) && pass;
	}

	/* Check behavior of GL_CLIP_PLANE0 + n where n == max_clip_planes */
	enum_value = GL_CLIP_PLANE0 + max_clip_planes;
	sprintf(enum_name, "GL_CLIP_PLANE0 + %d", max_clip_planes);

	printf("Trying glIsEnabled(%s): ", enum_name);
	b = glIsEnabled(enum_value);
	pass = (check_error(GL_INVALID_ENUM) && print_ok()) && pass;

	printf("Trying glGetBooleanv(%s): ", enum_name);
	glGetBooleanv(enum_value, &b);
	pass = (check_error(GL_INVALID_ENUM) && print_ok()) && pass;

	printf("Trying glEnable(%s): ", enum_name);
	glEnable(enum_value);
	pass = (check_error(GL_INVALID_ENUM) && print_ok()) && pass;

	printf("Trying glDisable(%s): ", enum_name);
	glDisable(enum_value);
	pass = (check_error(GL_INVALID_ENUM) && print_ok()) && pass;

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}