Add unittest.h with some assertion macros

This commit is contained in:
kichikuou
2020-07-24 12:46:14 +09:00
parent 55976a5816
commit fb10cbc52d
3 changed files with 158 additions and 92 deletions
+44 -45
View File
@@ -16,13 +16,12 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#undef NDEBUG
#include "list.h"
#include <assert.h>
#include "unittest.h"
#include <stdio.h>
static void list_func(void *data, void *user_data) {
assert(*(int*)data == ++*(int*)user_data);
ASSERT_EQUAL(*(int*)data, ++*(int*)user_data);
}
static int compare_func(const void *a, const void *b) {
@@ -36,18 +35,18 @@ int main() {
// Basic
{
SList* l = NULL;
assert(slist_length(l) == 0);
ASSERT_EQUAL(slist_length(l), 0);
l = slist_append(l, &data1);
assert(slist_length(l) == 1);
assert(l->data == &data1);
assert(slist_next(l) == NULL);
ASSERT_EQUAL(slist_length(l), 1);
ASSERT_EQUAL_PTR(l->data, &data1);
ASSERT_NULL(slist_next(l));
l = slist_append(l, &data2);
assert(slist_length(l) == 2);
assert(l->data == &data1);
assert(slist_next(l)->data == &data2);
assert(slist_next(slist_next(l)) == NULL);
ASSERT_EQUAL(slist_length(l), 2);
ASSERT_EQUAL_PTR(l->data, &data1);
ASSERT_EQUAL_PTR(slist_next(l)->data, &data2);
ASSERT_NULL(slist_next(slist_next(l)));
slist_free(l);
}
@@ -55,20 +54,20 @@ int main() {
// slist_nth, slist_last
{
SList* l = NULL;
assert(slist_nth(l, 0) == NULL);
assert(slist_nth(l, 1) == NULL);
assert(slist_last(l) == NULL);
ASSERT_NULL(slist_nth(l, 0));
ASSERT_NULL(slist_nth(l, 1));
ASSERT_NULL(slist_last(l));
l = slist_append(l, &data1);
assert(slist_nth(l, 0)->data == &data1);
assert(slist_nth(l, 1) == NULL);
assert(slist_last(l) == slist_nth(l, 0));
ASSERT_EQUAL_PTR(slist_nth(l, 0)->data, &data1);
ASSERT_NULL(slist_nth(l, 1));
ASSERT_EQUAL_PTR(slist_last(l), slist_nth(l, 0));
l = slist_append(l, &data2);
assert(slist_nth(l, 0)->data == &data1);
assert(slist_nth(l, 1)->data == &data2);
assert(slist_nth(l, 2) == NULL);
assert(slist_last(l) == slist_nth(l, 1));
ASSERT_EQUAL_PTR(slist_nth(l, 0)->data, &data1);
ASSERT_EQUAL_PTR(slist_nth(l, 1)->data, &data2);
ASSERT_NULL(slist_nth(l, 2));
ASSERT_EQUAL_PTR(slist_last(l), slist_nth(l, 1));
slist_free(l);
}
@@ -76,28 +75,28 @@ int main() {
// slist_remove
{
SList* l = NULL;
assert(slist_remove(l, NULL) == NULL);
assert(slist_remove(l, &data1) == NULL);
ASSERT_NULL(slist_remove(l, NULL));
ASSERT_NULL(slist_remove(l, &data1));
l = slist_append(l, &data1);
assert(slist_remove(l, NULL) == l);
ASSERT_EQUAL_PTR(slist_remove(l, NULL), l);
l = slist_remove(l, &data1);
assert(l == NULL);
ASSERT_NULL(l);
l = slist_append(l, &data1);
l = slist_append(l, &data2);
l = slist_append(l, &data1);
l = slist_append(l, &data2);
assert(slist_length(l) == 4);
ASSERT_EQUAL(slist_length(l), 4);
l = slist_remove(l, &data2);
assert(slist_length(l) == 3);
assert(slist_nth(l, 0)->data == &data1);
assert(slist_nth(l, 1)->data == &data1);
assert(slist_nth(l, 2)->data == &data2);
ASSERT_EQUAL(slist_length(l), 3);
ASSERT_EQUAL_PTR(slist_nth(l, 0)->data, &data1);
ASSERT_EQUAL_PTR(slist_nth(l, 1)->data, &data1);
ASSERT_EQUAL_PTR(slist_nth(l, 2)->data, &data2);
l = slist_remove(l, &data2);
assert(slist_length(l) == 2);
assert(slist_nth(l, 0)->data == &data1);
assert(slist_nth(l, 1)->data == &data1);
ASSERT_EQUAL(slist_length(l), 2);
ASSERT_EQUAL_PTR(slist_nth(l, 0)->data, &data1);
ASSERT_EQUAL_PTR(slist_nth(l, 1)->data, &data1);
slist_free(l);
}
@@ -105,17 +104,17 @@ int main() {
// slist_index
{
SList* l = NULL;
assert(slist_index(l, NULL) == -1);
assert(slist_index(l, &data1) == -1);
ASSERT_EQUAL(slist_index(l, NULL), -1);
ASSERT_EQUAL(slist_index(l, &data1), -1);
l = slist_append(l, &data1);
assert(slist_index(l, NULL) == -1);
assert(slist_index(l, &data1) == 0);
ASSERT_EQUAL(slist_index(l, NULL), -1);
ASSERT_EQUAL(slist_index(l, &data1), 0);
l = slist_append(l, &data1);
l = slist_append(l, &data2);
assert(slist_index(l, &data1) == 0);
assert(slist_index(l, &data2) == 2);
ASSERT_EQUAL(slist_index(l, &data1), 0);
ASSERT_EQUAL(slist_index(l, &data2), 2);
slist_free(l);
}
@@ -127,7 +126,7 @@ int main() {
l = slist_append(l, &data2);
int count = 0;
slist_foreach(l, list_func, &count);
assert(count == 2);
ASSERT_EQUAL(count, 2);
slist_free(l);
}
@@ -140,11 +139,11 @@ int main() {
l = slist_insert_sorted(l, &data3, compare_func);
l = slist_insert_sorted(l, &data0, compare_func);
l = slist_insert_sorted(l, &data2, compare_func);
assert(slist_length(l) == 4);
assert(slist_nth(l, 0)->data == &data0);
assert(slist_nth(l, 1)->data == &data1);
assert(slist_nth(l, 2)->data == &data2);
assert(slist_nth(l, 3)->data == &data3);
ASSERT_EQUAL(slist_length(l), 4);
ASSERT_EQUAL_PTR(slist_nth(l, 0)->data, &data0);
ASSERT_EQUAL_PTR(slist_nth(l, 1)->data, &data1);
ASSERT_EQUAL_PTR(slist_nth(l, 2)->data, &data2);
ASSERT_EQUAL_PTR(slist_nth(l, 3)->data, &data3);
slist_free(l);
}
+46 -47
View File
@@ -16,9 +16,8 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#undef NDEBUG
#include "gameresource.h"
#include <assert.h>
#include "unittest.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -26,40 +25,40 @@
static void initGameResource_test(void) {
GameResource gr;
assert(initGameResource(&gr, "testdata/test.gr"));
ASSERT_TRUE(initGameResource(&gr, "testdata/test.gr"));
assert(gr.cnt[DRIFILE_SCO] == 1);
assert(strcmp(gr.game_fname[DRIFILE_SCO][0], "fooSA.ALD") == 0);
assert(gr.cnt[DRIFILE_CG] == 2);
assert(strcmp(gr.game_fname[DRIFILE_CG][0], "fooGA.ALD") == 0);
assert(strcmp(gr.game_fname[DRIFILE_CG][1], "fooGB.ALD") == 0);
assert(gr.cnt[DRIFILE_WAVE] == 26);
assert(strcmp(gr.game_fname[DRIFILE_WAVE][0], "fooWA.ALD") == 0);
ASSERT_EQUAL(gr.cnt[DRIFILE_SCO], 1);
ASSERT_STRCMP(gr.game_fname[DRIFILE_SCO][0], "fooSA.ALD");
ASSERT_EQUAL(gr.cnt[DRIFILE_CG], 2);
ASSERT_STRCMP(gr.game_fname[DRIFILE_CG][0], "fooGA.ALD");
ASSERT_STRCMP(gr.game_fname[DRIFILE_CG][1], "fooGB.ALD");
ASSERT_EQUAL(gr.cnt[DRIFILE_WAVE], 26);
ASSERT_STRCMP(gr.game_fname[DRIFILE_WAVE][0], "fooWA.ALD");
for (int i = 1; i <= 24; i++)
assert(gr.game_fname[DRIFILE_WAVE][i] == NULL);
assert(strcmp(gr.game_fname[DRIFILE_WAVE][25], "fooWZ.ALD") == 0);
assert(gr.cnt[DRIFILE_MIDI] == 3);
assert(strcmp(gr.game_fname[DRIFILE_MIDI][2], "fooMC.ALD") == 0);
assert(strcmp(gr.game_fname[DRIFILE_DATA][0], "file name with whitespaces") == 0);
assert(strcmp(gr.game_fname[DRIFILE_RSC][0], "this line has whitespaces at end of line") == 0);
assert(strcmp(gr.game_fname[DRIFILE_BGM][0], "/path/to/fooBA.ALD") == 0);
ASSERT_NULL(gr.game_fname[DRIFILE_WAVE][i]);
ASSERT_STRCMP(gr.game_fname[DRIFILE_WAVE][25], "fooWZ.ALD");
ASSERT_EQUAL(gr.cnt[DRIFILE_MIDI], 3);
ASSERT_STRCMP(gr.game_fname[DRIFILE_MIDI][2], "fooMC.ALD");
ASSERT_STRCMP(gr.game_fname[DRIFILE_DATA][0], "file name with whitespaces");
ASSERT_STRCMP(gr.game_fname[DRIFILE_RSC][0], "this line has whitespaces at end of line");
ASSERT_STRCMP(gr.game_fname[DRIFILE_BGM][0], "/path/to/fooBA.ALD");
assert(strcmp(gr.ain, "System39.ain") == 0);
assert(strcmp(gr.wai, "foo_WA.WAI") == 0);
assert(strcmp(gr.bgi, "foo_BA.BGI") == 0);
assert(strcmp(gr.sact01, "SACTEFAM.KLD") == 0);
assert(strcmp(gr.init, "System39.ini") == 0);
assert(strcmp(gr.alk[0], "0.alk") == 0);
ASSERT_STRCMP(gr.ain, "System39.ain");
ASSERT_STRCMP(gr.wai, "foo_WA.WAI");
ASSERT_STRCMP(gr.bgi, "foo_BA.BGI");
ASSERT_STRCMP(gr.sact01, "SACTEFAM.KLD");
ASSERT_STRCMP(gr.init, "System39.ini");
ASSERT_STRCMP(gr.alk[0], "0.alk");
for (int i = 1; i <= 8; i++)
assert(gr.alk[i] == NULL);
assert(strcmp(gr.alk[9], "9.alk") == 0);
ASSERT_NULL(gr.alk[i]);
ASSERT_STRCMP(gr.alk[9], "9.alk");
assert(strcmp(gr.save_fname[0], "/home/kichikuou/save/foo_sa.asd") == 0);
ASSERT_STRCMP(gr.save_fname[0], "/home/kichikuou/save/foo_sa.asd");
for (int i = 1; i <= 24; i++)
assert(gr.save_fname[i] == NULL);
assert(strcmp(gr.save_fname[25], "/tmp/foo_sz.asd") == 0);
ASSERT_NULL(gr.save_fname[i]);
ASSERT_STRCMP(gr.save_fname[25], "/tmp/foo_sz.asd");
// save_path is determined based on save_fname[0].
assert(strcmp(gr.save_path, "/home/kichikuou/save") == 0);
ASSERT_STRCMP(gr.save_path, "/home/kichikuou/save");
}
static struct dirent *mockReaddir(DIR *dir) {
@@ -103,29 +102,29 @@ static void initGameResourceFromDir_test(void) {
static char cwd[256];
getcwd(cwd, sizeof(cwd));
GameResource gr;
assert(initGameResourceFromDir(&gr, (DIR *)&dir, mockReaddir));
assert(gr.cnt[DRIFILE_SCO] == 2);
assert(strcmp(gr.game_fname[DRIFILE_SCO][0], joinPath(cwd, "ADISK.ALD")) == 0);
assert(strcmp(gr.game_fname[DRIFILE_SCO][1], joinPath(cwd, "FOOSB.ALD")) == 0);
assert(gr.cnt[DRIFILE_CG] == 26);
assert(strcmp(gr.game_fname[DRIFILE_CG][25], joinPath(cwd, "foogz.ald")) == 0);
assert(gr.cnt[DRIFILE_WAVE] == 1);
assert(strcmp(gr.game_fname[DRIFILE_WAVE][0], joinPath(cwd, "WA.ALD")) == 0);
ASSERT_TRUE(initGameResourceFromDir(&gr, (DIR *)&dir, mockReaddir));
ASSERT_EQUAL(gr.cnt[DRIFILE_SCO], 2);
ASSERT_STRCMP(gr.game_fname[DRIFILE_SCO][0], joinPath(cwd, "ADISK.ALD"));
ASSERT_STRCMP(gr.game_fname[DRIFILE_SCO][1], joinPath(cwd, "FOOSB.ALD"));
ASSERT_EQUAL(gr.cnt[DRIFILE_CG], 26);
ASSERT_STRCMP(gr.game_fname[DRIFILE_CG][25], joinPath(cwd, "foogz.ald"));
ASSERT_EQUAL(gr.cnt[DRIFILE_WAVE], 1);
ASSERT_STRCMP(gr.game_fname[DRIFILE_WAVE][0], joinPath(cwd, "WA.ALD"));
for (int i = 0; i < 26; i++) {
char buf[16];
sprintf(buf, "%csleep.asd", 'a' + i);
assert(strcmp(gr.save_fname[i], joinPath(cwd, buf)) == 0);
ASSERT_STRCMP(gr.save_fname[i], joinPath(cwd, buf));
}
assert(strcmp(gr.save_path, cwd) == 0);
assert(strcmp(gr.ain, joinPath(cwd, "SYSTEM39.AIN")) == 0);
assert(strcmp(gr.wai, joinPath(cwd, "foo_WA.WAI")) == 0);
assert(strcmp(gr.bgi, joinPath(cwd, "foo_BA.BGI")) == 0);
assert(strcmp(gr.sact01, joinPath(cwd, "SACTEFAM.KLD")) == 0);
assert(strcmp(gr.init, joinPath(cwd, "System39.ini")) == 0);
assert(strcmp(gr.alk[0], joinPath(cwd, "0.alk")) == 0);
assert(strcmp(gr.alk[1], joinPath(cwd, "foo1.alk")) == 0);
ASSERT_STRCMP(gr.save_path, cwd);
ASSERT_STRCMP(gr.ain, joinPath(cwd, "SYSTEM39.AIN"));
ASSERT_STRCMP(gr.wai, joinPath(cwd, "foo_WA.WAI"));
ASSERT_STRCMP(gr.bgi, joinPath(cwd, "foo_BA.BGI"));
ASSERT_STRCMP(gr.sact01, joinPath(cwd, "SACTEFAM.KLD"));
ASSERT_STRCMP(gr.init, joinPath(cwd, "System39.ini"));
ASSERT_STRCMP(gr.alk[0], joinPath(cwd, "0.alk"));
ASSERT_STRCMP(gr.alk[1], joinPath(cwd, "foo1.alk"));
for (int i = 2; i < 10; i++)
assert(gr.alk[i] == NULL);
ASSERT_NULL(gr.alk[i]);
}
void gameresource_test(void) {
+68
View File
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2020 <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __UNITTEST_H__
#define __UNITTEST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ASSERT_TRUE(condition) assert_true(#condition, !!(condition), __FILE__, __LINE__)
#define ASSERT_FALSE(condition) assert_false(#condition, !!(condition), __FILE__, __LINE__)
#define ASSERT_NULL(actual) ASSERT_EQUAL_PTR(actual, NULL)
#define ASSERT_EQUAL(actual, expected) assert_equal_int(actual, expected, __FILE__, __LINE__)
#define ASSERT_EQUAL_PTR(actual, expected) assert_equal_ptr(actual, expected, __FILE__, __LINE__)
#define ASSERT_STRCMP(actual, expected) assert_strcmp(actual, expected, __FILE__, __LINE__)
static inline void assert_true(const char *condition, int actual, const char *file, int line) {
if (!actual) {
fprintf(stderr, "%s:%d assertion failed: %s\n", file, line, condition);
exit(1);
}
}
static inline void assert_false(const char *condition, int actual, const char *file, int line) {
if (actual) {
fprintf(stderr, "%s:%d expression unexpectedly evaluated to true: %s\n", file, line, condition);
exit(1);
}
}
static inline void assert_equal_int(int actual, int expected, const char *file, int line) {
if (expected != actual) {
fprintf(stderr, "%s:%d expected %d, got %d\n", file, line, expected, actual);
exit(1);
}
}
static inline void assert_equal_ptr(const void *actual, const void *expected, const char *file, int line) {
if (expected != actual) {
fprintf(stderr, "%s:%d expected %p, got %p\n", file, line, expected, actual);
exit(1);
}
}
static inline void assert_strcmp(const char *actual, const char *expected, const char *file, int line) {
if (!actual || strcmp(expected, actual)) {
fprintf(stderr, "%s:%d expected %s, got %s\n", file, line, expected, actual);
exit(1);
}
}
#endif /* __UNITTEST_H__ */