/* If <id> is zero, IsQueryARB should return FALSE.*/
static bool
conformOQ_IsIdZero(void)
{
	if (glIsQueryARB(0) == GL_TRUE) {
		printf(" Error: zero is treated as a valid id by glIsQueryARB().\n");
		return false;
	}

	return true;
}
/* If BeginQueryARB is called with an unused <id>, that name is marked as used
 * and associated with a new query object.
 */
static bool
conformOQ_Begin_unused_id(void)
{
	unsigned int id;
	bool pass = true;

	id = find_unused_id();

	if (id == 0)
		return false;

	glBeginQuery(GL_SAMPLES_PASSED_ARB, id);

	if (glIsQueryARB(id) == GL_FALSE) {
		printf("Error : Begin with a unused id failed.");
		pass = false;
	}

	glEndQuery(GL_SAMPLES_PASSED_ARB);

	return pass;
}
/* Basic tests on query id generation and deletion */
static bool
conformOQ_Gen_Delete(unsigned int id_n)
{
	GLuint *ids1 = NULL, *ids2 = NULL;
	unsigned int i, j;
	bool pass = true;

	ids1 = (GLuint *) malloc(id_n * sizeof(GLuint));
	ids2 = (GLuint *) malloc(id_n * sizeof(GLuint));

	if (!ids1 || !ids2) {
		printf(" Error: Cannot alloc memory to pointer ids[12].\n");
		if (ids1)
			free(ids1);
		if (ids2)
			free(ids2);
		return false;
	}

	glGenQueriesARB(id_n, ids1);
	glGenQueriesARB(id_n, ids2);

	/* compare whether <id> generated during the previous 2 rounds are
	 * duplicated */
	for (i = 0; i < id_n; i++) {
		for (j = 0; j < id_n; j++) {
			if (ids1[i] == ids2[j]) {
				char str[1000];
				sprintf(str, "ids1[%d] == ids2[%d] == %u.", i,
					j, ids1[i]);
				printf(" Error:  %s\n", str);
				pass = false;
			}
		}
	}

	/* Note: the spec seems to indicate that glGenQueries reserves query
	 * IDs but doesn't create query objects for those IDs.  A query object
	 * isn't created until they are used by glBeginQuery.  So this part
	 * of the test is invalid.
	 */
#if 0
	/* Checkout whether the Query ID just generated is valid */
	for (i = 0; i < id_n; i++) {
		if (glIsQueryARB(ids1[i]) == GL_FALSE) {
			char str[1000];
			sprintf(str, "id [%d] just generated is not valid.",
				ids1[i]);
			printf(" Error: %s\n", str);
			pass = false;
		}
	}
#endif

	/* if <id> is a non-zero value that is not the name of a query object,
	 * IsQueryARB returns FALSE.
	 */
	glDeleteQueriesARB(id_n, ids1);
	for (i = 0; i < id_n; i++) {
		if (glIsQueryARB(ids1[i]) == GL_TRUE) {
			char str[1000];
			sprintf(str, "id [%d] just deleted is still valid.",
				ids1[i]);
			printf(" Error: %s\n", str);
			pass = false;
		}
	}

	/* Delete only for sanity purpose */
	glDeleteQueriesARB(id_n, ids2);

	if (ids1)
		free(ids1);
	if (ids2)
		free(ids2);


	ids1 = (GLuint *) malloc(id_n * sizeof(GLuint));
	if (ids1 == NULL)
		return false;

	for (i = 0; i < id_n; i++) {
		glGenQueriesARB(1, ids1 + i);
		for (j = 0; j < i; j++) {
			if (ids1[i] == ids1[j]) {
				char str[1000];
				sprintf(str, "duplicated id generated [%u]",
					ids1[i]);
				printf(" Error: %s\n", str);
				pass = false;
			}
		}
	}

	glDeleteQueriesARB(id_n, ids1);
	if (ids1)
		free(ids1);

	return pass;
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_ARBOcclusionQuery_nglIsQueryARB(JNIEnv *env, jclass clazz, jint id, jlong function_pointer) {
	glIsQueryARBPROC glIsQueryARB = (glIsQueryARBPROC)((intptr_t)function_pointer);
	GLboolean __result = glIsQueryARB(id);
	return __result;
}