From 2c1db601a66ad27e0f8e19ad54048c84d70a1775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Fri, 5 Apr 2024 02:14:48 +0900 Subject: [PATCH] chore(test): add documents --- test/coder_test.c | 18 ++++++++++ test/file_test.c | 71 ++++++++++----------------------------- test/file_test.h | 85 +++++++++++++++++++++++++++++++++++++++++++++++ test/wrap_test.c | 71 ++++++++++----------------------------- 4 files changed, 139 insertions(+), 106 deletions(-) create mode 100644 test/file_test.h diff --git a/test/coder_test.c b/test/coder_test.c index 10d7769..d20674a 100644 --- a/test/coder_test.c +++ b/test/coder_test.c @@ -1,3 +1,21 @@ +/* test/coder_test.c + * This file is part of the base16384 distribution (https://github.com/fumiama/base16384). + * Copyright (c) 2022-2024 Fumiama Minamoto. + * + * 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 3 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, see . + */ + #include #include #include diff --git a/test/file_test.c b/test/file_test.c index 2107bca..9382a81 100644 --- a/test/file_test.c +++ b/test/file_test.c @@ -1,3 +1,20 @@ +/* test/file_test.c + * This file is part of the base16384 distribution (https://github.com/fumiama/base16384). + * Copyright (c) 2022-2024 Fumiama Minamoto. + * + * 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 3 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, see . + */ #ifdef _WIN32 #include @@ -15,6 +32,7 @@ #include "base16384.h" #include "binary.h" +#include "file_test.h" #define TEST_SIZE (4096) #define TEST_INPUT_FILENAME "file_test_input.bin" @@ -25,59 +43,6 @@ char encbuf[BASE16384_ENCBUFSZ]; char decbuf[BASE16384_DECBUFSZ]; char tstbuf[BASE16384_ENCBUFSZ]; -#define ok(has_failed, reason) \ - if (has_failed) { \ - perror(reason); \ - return 1; \ - } - -#define loop_ok(has_failed, i, reason) \ - if (has_failed) { \ - fprintf(stderr, "loop @%d: ", i); \ - perror(reason); \ - return 1; \ - } - -#define reset_and_truncate(fd, i) { \ - fd = open(TEST_INPUT_FILENAME, O_RDWR); \ - ok(!fd, "open"); \ - loop_ok(lseek(fd, 0, SEEK_SET), i, "lseek"); \ - loop_ok(ftruncate(fd, i), i, "ftruncate"); \ -} - -#define base16384_loop_ok(err) \ - if (err) { \ - fprintf(stderr, "loop @%d: ", i); \ - base16384_perror(err); \ - return 1; \ - } - -#define validate_result() \ - uint64_t buf, sum_input = 0, sum_validate = 0; \ - fp = fopen(TEST_INPUT_FILENAME, "rb"); { \ - loop_ok(!fp, i, "fopen"); \ - while (fread(&buf, sizeof(sum_input), 1, fp) > 0) sum_input += buf; \ - buf = 0; \ - while (fread(&buf, 1, 1, fp) > 0) { \ - sum_input += buf; \ - sum_input = LEFTROTATE(sum_input, 4); \ - } \ - } fclose(fp); \ - fp = fopen(TEST_VALIDATE_FILENAME, "rb"); { \ - loop_ok(!fp, i, "fopen"); \ - while (fread(&buf, sizeof(sum_validate), 1, fp) > 0) sum_validate += buf; \ - buf = 0; \ - while (fread(&buf, 1, 1, fp) > 0) { \ - sum_validate += buf; \ - sum_validate = LEFTROTATE(sum_validate, 4); \ - } \ - } fclose(fp); \ - if (sum_input != sum_validate) { \ - fprintf(stderr, "loop @%d, expect: %016llx, got: %016llx: ", i, (unsigned long long)sum_input, (unsigned long long)sum_validate); \ - fputs(TEST_INPUT_FILENAME " and " TEST_VALIDATE_FILENAME " mismatch.", stderr); \ - return 1; \ - } - #define init_input_file() \ for(i = 0; i < TEST_SIZE; i += sizeof(int)) { \ *(int*)(&encbuf[i]) = rand(); \ diff --git a/test/file_test.h b/test/file_test.h new file mode 100644 index 0000000..fd97f4d --- /dev/null +++ b/test/file_test.h @@ -0,0 +1,85 @@ +#ifndef _FILE_TEST_H_ +#define _FILE_TEST_H_ + +/* test/file_test.h + * This file is part of the base16384 distribution (https://github.com/fumiama/base16384). + * Copyright (c) 2022-2024 Fumiama Minamoto. + * + * 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 3 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, see . + */ + +#define ok(has_failed, reason) \ + if (has_failed) { \ + perror(reason); \ + return 1; \ + } + +#define loop_ok(has_failed, i, reason) \ + if (has_failed) { \ + fprintf(stderr, "loop @%d: ", i); \ + perror(reason); \ + return 1; \ + } + +#define reset_and_truncate(fd, i) { \ + fd = open(TEST_INPUT_FILENAME, O_RDWR); \ + ok(!fd, "open"); \ + loop_ok(lseek(fd, 0, SEEK_SET), i, "lseek"); \ + loop_ok(ftruncate(fd, i), i, "ftruncate"); \ +} + +#define base16384_loop_ok(err) \ + if (err) { \ + fprintf(stderr, "loop @%d: ", i); \ + base16384_perror(err); \ + return 1; \ + } + +#define validate_result() \ + uint64_t buf, sum_input = 0, sum_validate = 0; \ + fp = fopen(TEST_INPUT_FILENAME, "rb"); { \ + loop_ok(!fp, i, "fopen"); \ + int cnt; \ + while ((cnt = fread(&buf, 1, sizeof(sum_input), fp)) > 0) { \ + int n; \ + buf = 0; \ + while(cnt < sizeof(sum_input)) { \ + n = fread((uint8_t*)(&buf)+cnt, 1, 1, fp); \ + if (n) cnt++; \ + else break; \ + } \ + sum_input += buf; \ + } \ + } fclose(fp); \ + fp = fopen(TEST_VALIDATE_FILENAME, "rb"); { \ + loop_ok(!fp, i, "fopen"); \ + int cnt; \ + while ((cnt = fread(&buf, 1, sizeof(sum_validate), fp)) > 0) { \ + int n; \ + buf = 0; \ + while(cnt < sizeof(sum_validate)) { \ + n = fread((uint8_t*)(&buf)+cnt, 1, 1, fp); \ + if (n) cnt++; \ + else break; \ + } \ + sum_validate += buf; \ + } \ + } fclose(fp); \ + if (sum_input != sum_validate) { \ + fprintf(stderr, "loop @%d, expect: %016llx, got: %016llx: ", i, (unsigned long long)sum_input, (unsigned long long)sum_validate); \ + fputs(TEST_INPUT_FILENAME " and " TEST_VALIDATE_FILENAME " mismatch.", stderr); \ + return 1; \ + } + +#endif \ No newline at end of file diff --git a/test/wrap_test.c b/test/wrap_test.c index 4bc5358..095c40d 100644 --- a/test/wrap_test.c +++ b/test/wrap_test.c @@ -1,3 +1,20 @@ +/* test/wrap_test.c + * This file is part of the base16384 distribution (https://github.com/fumiama/base16384). + * Copyright (c) 2022-2024 Fumiama Minamoto. + * + * 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 3 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, see . + */ #ifdef _WIN32 #include @@ -15,6 +32,7 @@ #include "base16384.h" #include "binary.h" +#include "file_test.h" #define TEST_SIZE (4096) #define TEST_INPUT_FILENAME "wrap_test_input.bin" @@ -25,59 +43,6 @@ char encbuf[BASE16384_ENCBUFSZ]; char decbuf[BASE16384_DECBUFSZ]; char tstbuf[BASE16384_ENCBUFSZ]; -#define ok(has_failed, reason) \ - if (has_failed) { \ - perror(reason); \ - return 1; \ - } - -#define loop_ok(has_failed, i, reason) \ - if (has_failed) { \ - fprintf(stderr, "loop @%d: ", i); \ - perror(reason); \ - return 1; \ - } - -#define reset_and_truncate(fd, i) { \ - fd = open(TEST_INPUT_FILENAME, O_RDWR); \ - ok(!fd, "open"); \ - loop_ok(lseek(fd, 0, SEEK_SET), i, "lseek"); \ - loop_ok(ftruncate(fd, i), i, "ftruncate"); \ -} - -#define base16384_loop_ok(err) \ - if (err) { \ - fprintf(stderr, "loop @%d: ", i); \ - base16384_perror(err); \ - return 1; \ - } - -#define validate_result() \ - uint64_t buf, sum_input = 0, sum_validate = 0; \ - fp = fopen(TEST_INPUT_FILENAME, "rb"); { \ - loop_ok(!fp, i, "fopen"); \ - while (fread(&buf, sizeof(sum_input), 1, fp) > 0) sum_input += buf; \ - buf = 0; \ - while (fread(&buf, 1, 1, fp) > 0) { \ - sum_input += buf; \ - sum_input = LEFTROTATE(sum_input, 4); \ - } \ - } fclose(fp); \ - fp = fopen(TEST_VALIDATE_FILENAME, "rb"); { \ - loop_ok(!fp, i, "fopen"); \ - while (fread(&buf, sizeof(sum_validate), 1, fp) > 0) sum_validate += buf; \ - buf = 0; \ - while (fread(&buf, 1, 1, fp) > 0) { \ - sum_validate += buf; \ - sum_validate = LEFTROTATE(sum_validate, 4); \ - } \ - } fclose(fp); \ - if (sum_input != sum_validate) { \ - fprintf(stderr, "loop @%d, expect: %016llx, got: %016llx: ", i, (unsigned long long)sum_input, (unsigned long long)sum_validate); \ - fputs(TEST_INPUT_FILENAME " and " TEST_VALIDATE_FILENAME " mismatch.", stderr); \ - return 1; \ - } - #define init_input_file() \ for(i = 0; i < TEST_SIZE; i += sizeof(int)) { \ *(int*)(&encbuf[i]) = rand(); \