| /* |
| * 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 <stdbool.h> |
| #include <stdio.h> |
| |
| static unsigned int _tests_total = 0; /* Number of conditions checked */ |
| static unsigned int _tests_failed = 0; /* Number of conditions failed */ |
| |
| #ifdef USER_TASK_WITH_TRUSTY_USER_BASE_LIB_UNITTEST |
| #include <lib/unittest/unittest.h> |
| #else |
| static inline int unittest_printf(const char* fmt, ...) { |
| return 0; |
| } |
| #endif |
| |
| #define TLOGI(fmt, ...) \ |
| do { \ |
| fprintf(stderr, "%s: %d: " fmt, LOG_TAG, __LINE__, ##__VA_ARGS__); \ |
| unittest_printf("%s: %d: " fmt, LOG_TAG, __LINE__, ##__VA_ARGS__); \ |
| } while (0) |
| |
| /* |
| * 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 \ |
| " (%ld), " \ |
| "actual " #actual " (%ld)\n", \ |
| msg, (long)_e, (long)_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 \ |
| " (%ld), " \ |
| "actual " #actual " (%ld)\n", \ |
| msg, (long)_e, (long)_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 \ |
| " (%ld), " \ |
| "actual " #actual " (%ld)\n", \ |
| msg, (long)_e, (long)_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 " (%ld)\n", \ |
| msg, (long)_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 " (%ld)\n", \ |
| msg, (long)_a); \ |
| _tests_failed++; \ |
| _all_ok = false; \ |
| } \ |
| } |