blob: 1b18743dcdbb7bbcb2295179b06da34558a4321a [file] [log] [blame]
/*
* Copyright (C) 2014-2015 The Android Open Source Project
*
* 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.
*/
// TODO: move this file to a better location
#pragma once
#include <err.h>
#include <stdbool.h>
#include <stdio.h>
static uint _tests_total = 0; /* Number of conditions checked */
static uint _tests_failed = 0; /* Number of conditions failed */
#define TLOGI(fmt, ...) \
fprintf(stderr, "%s: %d: " fmt, LOG_TAG, __LINE__, ## __VA_ARGS__)
/*
* Begin and end test macro
*/
#define TEST_BEGIN(name) \
bool _all_ok = true; \
const char *_test = name; \
TLOGI("%s:\n", _test);
#define TEST_END \
{ \
if (_all_ok) \
TLOGI("%s: PASSED\n", _test); \
else \
TLOGI("%s: FAILED\n", _test); \
}
/*
* EXPECT_* macros to check test results.
*/
#define EXPECT_EQ(expected, actual, msg) \
{ \
typeof(actual) _e = expected; \
typeof(actual) _a = actual; \
_tests_total++; \
if (_e != _a) { \
TLOGI("%s: expected " #expected " (%d), " \
"actual " #actual " (%d)\n", \
msg, (int)_e, (int)_a); \
_tests_failed++; \
_all_ok = false; \
} \
}
#define EXPECT_NE(expected, actual, msg) \
{ \
typeof(actual) _e = expected; \
typeof(actual) _a = actual; \
_tests_total++; \
if (_e == _a) { \
TLOGI("%s: expected not " #expected " (%d), " \
"actual " #actual " (%d)\n", \
msg, (int)_e, (int)_a); \
_tests_failed++; \
_all_ok = false; \
} \
}
#define EXPECT_GT(expected, actual, msg) \
{ \
typeof(actual) _e = expected; \
typeof(actual) _a = actual; \
_tests_total++; \
if (_e <= _a) { \
TLOGI("%s: expected " #expected " (%d), " \
"actual " #actual " (%d)\n", \
msg, (int)_e, (int)_a); \
_tests_failed++; \
_all_ok = false; \
} \
}
#define EXPECT_GE_ZERO(actual, msg) \
{ \
typeof(actual) _a = actual; \
_tests_total++; \
if (_a < 0) { \
TLOGI("%s: expected >= 0 " \
"actual " #actual " (%d)\n", msg, (int)_a); \
_tests_failed++; \
_all_ok = false; \
} \
}
#define EXPECT_GT_ZERO(actual, msg) \
{ \
typeof(actual) _a = actual; \
_tests_total++; \
if (_a <= 0) { \
TLOGI("%s: expected > 0 " \
"actual " #actual " (%d)\n", msg, (int)_a); \
_tests_failed++; \
_all_ok = false; \
} \
}