/* Copyright 2016 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* invert assert behaviour for checking negative reaction */
#if (defined(SCL_USE_ASSERT_H) && (SCL_USE_ASSERT_H == 0)) || defined(NDEBUG)
#	define ASSERT() do { \
		if (asserts != 1) SCL_ABORT(); \
		asserts = 0; \
	} while (0 > 1)
#else
#	define ASSERT(assertion) do { assert(asserts == 1); asserts = 0; } while (0 > 1)
#endif

static int asserts;

static void scl_assert(int assertion) { asserts += (int)!(assertion); }

#define SCL_ASSERT(assertion) scl_assert(assertion)

#include "scl/def.h"
#include <stdio.h>


static void do_tests(void) {
	int volatile buf1[16] = {0};
	int 	i[3];
	long	l[4];
	short	s[5];
	char	c[6];
	short	a[256];
	char	b[256 / sizeof(char)];
	int volatile buf2[16] = {0};
	volatile long ignore;
	volatile int m1, ind;
	if (buf1[0] != buf2[0]) {
		SCL_ASSERT(0 > 1);
	}

	asserts = 0;
	ignore = scl_index(3, 3);
	ASSERT();
	(void)ignore;
	m1 = -1;
	(void)SCL_ARRAY_AT(i, m1);
	ASSERT();

	ind = 4;
	(void)SCL_ARRAY_AT(l, ind);
	ASSERT();
	ignore = scl_index(4, -1);
	ASSERT();
	(void)ignore;

	ignore = scl_index(5, 5);
	ASSERT();
	(void)ignore;
	(void)SCL_ARRAY_AT(s, m1);
	ASSERT();

	ind = 6;
	(void)SCL_ARRAY_AT(c, ind);
	ASSERT();
	ignore = scl_index(6, -1);
	ASSERT();
	(void)ignore;

#	undef SCL_SIZE_MAX
#	define SCL_SIZE_MAX 256
	ignore = scl_index(256 / sizeof(char), 256 / sizeof(char));
	ASSERT();
	(void)ignore;
	(void)SCL_ARRAY_AT(b, m1);
	ASSERT();

	ind = 256;
	(void)SCL_ARRAY_AT(a, ind);
	ASSERT();
	ignore = scl_index(256, -1);
	ASSERT();
	(void)ignore;
}

#if defined(START_FUNC) && (START_FUNC == 1)
#	undef START_FUNC
#	define START_FUNC test_safe_indexing_negative
#endif

#if defined(START_FUNC)
	extern void START_FUNC(void) {
		do_tests();
	}
#else
	extern int main(int argc, char const **argv) {
		do_tests();
		return 0;
	}
#endif
