migrate from munit test to simple macros

This commit is contained in:
Gregory 2019-03-02 20:52:33 +02:00
parent 924c10f5da
commit de73b25502
15 changed files with 216 additions and 3449 deletions

View file

@ -43,9 +43,6 @@ LIBFT_INC := -I $(LIBFT_DIR)includes/
LIBFT_LIB := -lft -L $(LIBFT_DIR)
LIBFT = $(LIBFT_DIR)libft.a
MUINUT_DIR := $(LIB_DIR)munit/
MUINUT_INC := -I $(MUINUT_DIR)
# project source files
MD5_SRC = ft_md5.c \
@ -126,9 +123,7 @@ SHA_TESTS += $(SHA_SRC)
BASE64_TESTS = base64_tests.c
BASE64_TESTS += $(BASE64_SRC)
TEST_SRC = tests.c \
munit.c
TEST_SRC = tests.c
TEST_SRC += $(MD5_TESTS) \
$(SHA_TESTS) \
$(BASE64_TESTS)
@ -221,8 +216,6 @@ vpath %.c $(SRC_DIR) \
$(MD5_DIR) \
$(SHA_DIR) \
$(B64_DIR) \
$(TST_DIR) \
$(MUINUT_DIR)
$(TST_DIR)
.PHONY: all check clean fclean re multi

24
inc/t.h Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* t.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/23 19:36:58 by gtertysh #+# #+# */
/* Updated: 2018/10/23 19:40:12 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef T_H
# define T_H
# include <stdio.h>
# define FAIL() printf("\nfail in %s() %s:%d\n\n", __func__, __FILE__, __LINE__)
# define _is(t) do { if (!(t)) { FAIL(); return 1; } } while(0)
# define _should(t) do { int r = t(); if(r) return r; } while(0)
# define _p_start(m) do { printf("%s\n", m); } while(0)
# define _verify(m, t) do { _p_start(m); int r = t(); if(r) return r; } while(0)
# define _end(m) do { printf(" %s - OK\n", m); return 0; } while(0)
#endif

View file

@ -13,65 +13,8 @@
#ifndef TESTS_H
# define TESTS_H
# define MUNIT_ENABLE_ASSERT_ALIASES
# include "munit.h"
int md5_tests(void);
int sha_tests(void);
int base64_tests(void);
/*
** MD5
*/
MunitResult should_init_md5_ctx(const MunitParameter test_params[],
void *test_data);
MunitResult md5_update_change_count(const MunitParameter test_params[],
void *test_data);
MunitResult md5_decode_string_to_int(const MunitParameter test_params[],
void *test_data);
MunitResult md5_encode_bits_to_string(const MunitParameter test_params[],
void *test_data);
MunitResult md5_encode_register(const MunitParameter test_params[],
void *test_data);
MunitResult md5_create_digest(const MunitParameter test_params[],
void *test_data);
MunitResult md5_create_string(const MunitParameter test_params[],
void *test_data);
/*
** SHA256
*/
MunitResult should_init_ctx_sha256(const MunitParameter test_params[],
void *test_data);
MunitResult decode_string_to_int_sha256(const MunitParameter test_params[],
void *test_data);
MunitResult encode_len_to_string_sha256(const MunitParameter test_params[],
void *test_data);
MunitResult encode_register_to_string_sha256(const MunitParameter test_params[],
void *test_data);
MunitResult update_bit_count_sha256(const MunitParameter test_params[],
void *test_data);
MunitResult fill_buffer_sha256(const MunitParameter test_params[],
void *test_data);
MunitResult add_right_padding_sha256(const MunitParameter test_params[],
void *test_data);
MunitResult compute_digest_sha256(const MunitParameter test_params[],
void *test_data);
MunitResult create_digest_string_sha256(const MunitParameter test_params[],
void *test_data);
/*
** SHA224
*/
MunitResult should_init_ctx_sha224(const MunitParameter test_params[],
void *test_data);
MunitResult compute_digest_sha224(const MunitParameter test_params[],
void *test_data);
MunitResult create_digest_string_sha224(const MunitParameter test_params[],
void *test_data);
/*
** BASE64
*/
MunitResult should_init_base64_ctx(const MunitParameter test_params[],
void *test_data);
MunitResult should_transform_base64_block(const MunitParameter test_params[],
void *test_data);
#endif

View file

@ -1,34 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_macros.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/23 19:46:23 by gtertysh #+# #+# */
/* Updated: 2018/10/23 19:46:33 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TESTS_MACROS_H
# define TESTS_MACROS_H
# include "munit.h"
# define UNUSED(x) (void)(x)
# define TEST_RESULT MunitResult
# define TEST_PARAMS const MunitParameter test_params[]
# define TEST_DATA void *test_data
# define CASE(f) TEST_RESULT f(TEST_PARAMS, TEST_DATA)
# define N(name) (char *)name
# define T(test_function) test_function
# define S(setup_function) setup_function
# define TD(tear_down_function) tear_down_function
# define O(options) options
# define P(paramenters) paramenters
# define IT(n, t, s, td, o, p) { N(n), T(t), S(s), TD(td), O(o), P(p) }
# define END_IT IT(NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL)
#endif

View file

@ -1,21 +0,0 @@
µnit Testing Framework
Copyright (c) 2013-2016 Evan Nemerson <evan@nemerson.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -1,55 +0,0 @@
# Using µnit is very simple; just include the header and add the C
# file to your sources. That said, here is a simple Makefile to build
# the example.
CSTD:=99
OPENMP:=n
ASAN:=n
UBSAN:=n
EXTENSION:=
TEST_ENV:=
CFLAGS:=
AGGRESSIVE_WARNINGS=n
ifeq ($(CC),pgcc)
CFLAGS+=-c$(CSTD)
else
CFLAGS+=-std=c$(CSTD)
endif
ifeq ($(OPENMP),y)
ifeq ($(CC),pgcc)
CFLAGS+=-mp
else
CFLAGS+=-fopenmp
endif
endif
ifneq ($(SANITIZER),)
CFLAGS+=-fsanitize=$(SANITIZER)
endif
ifneq ($(CC),pgcc)
ifeq ($(EXTRA_WARNINGS),y)
CFLAGS+=-Wall -Wextra -Werror
endif
ifeq ($(ASAN),y)
CFLAGS+=-fsanitize=address
endif
ifeq ($(UBSAN),y)
CFLAGS+=-fsanitize=undefined
endif
endif
example$(EXTENSION): munit.h munit.c example.c
$(CC) $(CFLAGS) -o $@ munit.c example.c
test:
$(TEST_ENV) ./example$(EXTENSION)
clean:
rm -f example$(EXTENSION)
all: example$(EXTENSION)

View file

@ -1,54 +0,0 @@
# µnit
µnit is a small but full-featured unit testing framework for C. It has
no dependencies (beyond libc), is permissively licensed (MIT), and is
easy to include into any project.
For more information, see
[the µnit web site](https://nemequ.github.io/munit).
[![Build status](https://travis-ci.org/nemequ/munit.svg?branch=master)](https://travis-ci.org/nemequ/munit)
[![Windows build status](https://ci.appveyor.com/api/projects/status/db515g5ifcwjohq7/branch/master?svg=true)](https://ci.appveyor.com/project/quixdb/munit/branch/master)
## Features
Features µnit currently includes include:
* Handy assertion macros which make for nice error messages.
* Reproducible cross-platform random number generation, including
support for supplying a seed via CLI.
* Timing of both wall-clock and CPU time.
* Parameterized tests.
* Nested test suites.
* Flexible CLI.
* Forking
([except on Windows](https://github.com/nemequ/munit/issues/2)).
* Hiding output of successful tests.
Features µnit does not currently include, but some day may include
(a.k.a., if you file a PR…), include:
* [TAP](http://testanything.org/) support; feel free to discuss in
[issue #1](https://github.com/nemequ/munit/issues/1)
### Include into your project with meson
In your `subprojects` folder put a `munit.wrap` file containing:
```
[wrap-git]
directory=munit
url=https://github.com/nemequ/munit/
revision=head
```
Then you can use a subproject fallback when you include munit as a
dependency to your project: `dependency('munit', fallback: ['munit', 'munit_dep'])`
## Documentation
See [the µnit web site](https://nemequ.github.io/munit).
Additionally, there is a heavily-commented
[example.c](https://github.com/nemequ/munit/blob/master/example.c) in
the repository.

View file

@ -1,351 +0,0 @@
/* Example file for using µnit.
*
* µnit is MIT-licensed, but for this file and this file alone:
*
* To the extent possible under law, the author(s) of this file have
* waived all copyright and related or neighboring rights to this
* work. See <https://creativecommons.org/publicdomain/zero/1.0/> for
* details.
*********************************************************************/
#include "munit.h"
/* This is just to disable an MSVC warning about conditional
* expressions being constant, which you shouldn't have to do for your
* code. It's only here because we want to be able to do silly things
* like assert that 0 != 1 for our demo. */
#if defined(_MSC_VER)
#pragma warning(disable: 4127)
#endif
/* Tests are functions that return void, and take a single void*
* parameter. We'll get to what that parameter is later. */
static MunitResult
test_compare(const MunitParameter params[], void* data) {
/* We'll use these later */
const unsigned char val_uchar = 'b';
const short val_short = 1729;
double pi = 3.141592654;
char* stewardesses = "stewardesses";
char* most_fun_word_to_type;
/* These are just to silence compiler warnings about the parameters
* being unused. */
(void) params;
(void) data;
/* Let's start with the basics. */
munit_assert(0 != 1);
/* There is also the more verbose, though slightly more descriptive
munit_assert_true/false: */
munit_assert_false(0);
/* You can also call munit_error and munit_errorf yourself. We
* won't do it is used to indicate a failure, but here is what it
* would look like: */
/* munit_error("FAIL"); */
/* munit_errorf("Goodbye, cruel %s", "world"); */
/* There are macros for comparing lots of types. */
munit_assert_char('a', ==, 'a');
/* Sure, you could just assert('a' == 'a'), but if you did that, a
* failed assertion would just say something like "assertion failed:
* val_uchar == 'b'". µnit will tell you the actual values, so a
* failure here would result in something like "assertion failed:
* val_uchar == 'b' ('X' == 'b')." */
munit_assert_uchar(val_uchar, ==, 'b');
/* Obviously we can handle values larger than 'char' and 'uchar'.
* There are versions for char, short, int, long, long long,
* int8/16/32/64_t, as well as the unsigned versions of them all. */
munit_assert_short(42, <, val_short);
/* There is also support for size_t.
*
* The longest word in English without repeating any letters is
* "uncopyrightables", which has uncopyrightable (and
* dermatoglyphics, which is the study of fingerprints) beat by a
* character */
munit_assert_size(strlen("uncopyrightables"), >, strlen("dermatoglyphics"));
/* Of course there is also support for doubles and floats. */
munit_assert_double(pi, ==, 3.141592654);
/* If you want to compare two doubles for equality, you might want
* to consider using munit_assert_double_equal. It compares two
* doubles for equality within a precison of 1.0 x 10^-(precision).
* Note that precision (the third argument to the macro) needs to be
* fully evaluated to an integer by the preprocessor so µnit doesn't
* have to depend pow, which is often in libm not libc. */
munit_assert_double_equal(3.141592654, 3.141592653589793, 9);
/* And if you want to check strings for equality (or inequality),
* there is munit_assert_string_equal/not_equal.
*
* "stewardesses" is the longest word you can type on a QWERTY
* keyboard with only one hand, which makes it loads of fun to type.
* If I'm going to have to type a string repeatedly, let's make it a
* good one! */
munit_assert_string_equal(stewardesses, "stewardesses");
/* A personal favorite macro which is fantastic if you're working
* with binary data, is the one which naïvely checks two blobs of
* memory for equality. If this fails it will tell you the offset
* of the first differing byte. */
munit_assert_memory_equal(7, stewardesses, "steward");
/* You can also make sure that two blobs differ *somewhere*: */
munit_assert_memory_not_equal(8, stewardesses, "steward");
/* There are equal/not_equal macros for pointers, too: */
most_fun_word_to_type = stewardesses;
munit_assert_ptr_equal(most_fun_word_to_type, stewardesses);
/* And null/not_null */
munit_assert_null(NULL);
munit_assert_not_null(most_fun_word_to_type);
/* Lets verify that the data parameter is what we expected. We'll
* see where this comes from in a bit.
*
* Note that the casting isn't usually required; if you give this
* function a real pointer (instead of a number like 0xdeadbeef) it
* would work as expected. */
munit_assert_ptr_equal(data, (void*)(uintptr_t)0xdeadbeef);
return MUNIT_OK;
}
static MunitResult
test_rand(const MunitParameter params[], void* user_data) {
int random_int;
double random_dbl;
munit_uint8_t data[5];
(void) params;
(void) user_data;
/* One thing missing from a lot of unit testing frameworks is a
* random number generator. You can't just use srand/rand because
* the implementation varies across different platforms, and it's
* important to be able to look at the seed used in a failing test
* to see if you can reproduce it. Some randomness is a fantastic
* thing to have in your tests, I don't know why more people don't
* do it...
*
* µnit's PRNG is re-seeded with the same value for each iteration
* of each test. The seed is retrieved from the MUNIT_SEED
* envirnment variable or, if none is provided, one will be
* (pseudo-)randomly generated. */
/* If you need an integer in a given range */
random_int = munit_rand_int_range(128, 4096);
munit_assert_int(random_int, >=, 128);
munit_assert_int(random_int, <=, 4096);
/* Or maybe you want a double, between 0 and 1: */
random_dbl = munit_rand_double();
munit_assert_double(random_dbl, >=, 0.0);
munit_assert_double(random_dbl, <=, 1.0);
/* Of course, you want to be able to reproduce bugs discovered
* during testing, so every time the tests are run they print the
* random seed used. When you want to reproduce a result, just put
* that random seed in the MUNIT_SEED environment variable; it even
* works on different platforms.
*
* If you want this to pass, use 0xdeadbeef as the random seed and
* uncomment the next line of code. Note that the PRNG is not
* re-seeded between iterations of the same test, so this will only
* work on the first iteration. */
/* munit_assert_uint32(munit_rand_uint32(), ==, 1306447409); */
/* You can also get blobs of random memory: */
munit_rand_memory(sizeof(data), data);
return MUNIT_OK;
}
/* This test case shows how to accept parameters. We'll see how to
* specify them soon.
*
* By default, every possible variation of a parameterized test is
* run, but you can specify parameters manually if you want to only
* run specific test(s), or you can pass the --single argument to the
* CLI to have the harness simply choose one variation at random
* instead of running them all. */
static MunitResult
test_parameters(const MunitParameter params[], void* user_data) {
const char* foo;
const char* bar;
(void) user_data;
/* The "foo" parameter is specified as one of the following values:
* "one", "two", or "three". */
foo = munit_parameters_get(params, "foo");
/* Similarly, "bar" is one of "four", "five", or "six". */
bar = munit_parameters_get(params, "bar");
/* "baz" is a bit more complicated. We don't actually specify a
* list of valid values, so by default NULL is passed. However, the
* CLI will accept any value. This is a good way to have a value
* that is usually selected randomly by the test, but can be
* overridden on the command line if desired. */
/* const char* baz = munit_parameters_get(params, "baz"); */
/* Notice that we're returning MUNIT_FAIL instead of writing an
* error message. Error messages are generally preferable, since
* they make it easier to diagnose the issue, but this is an
* option.
*
* Possible values are:
* - MUNIT_OK: Sucess
* - MUNIT_FAIL: Failure
* - MUNIT_SKIP: The test was skipped; usually this happens when a
* particular feature isn't in use. For example, if you're
* writing a test which uses a Wayland-only feature, but your
* application is running on X11.
* - MUNIT_ERROR: The test failed, but not because of anything you
* wanted to test. For example, maybe your test downloads a
* remote resource and tries to parse it, but the network was
* down.
*/
if (strcmp(foo, "one") != 0 &&
strcmp(foo, "two") != 0 &&
strcmp(foo, "three") != 0)
return MUNIT_FAIL;
if (strcmp(bar, "red") != 0 &&
strcmp(bar, "green") != 0 &&
strcmp(bar, "blue") != 0)
return MUNIT_FAIL;
return MUNIT_OK;
}
/* The setup function, if you provide one, for a test will be run
* before the test, and the return value will be passed as the sole
* parameter to the test function. */
static void*
test_compare_setup(const MunitParameter params[], void* user_data) {
(void) params;
munit_assert_string_equal(user_data, "µnit");
return (void*) (uintptr_t) 0xdeadbeef;
}
/* To clean up after a test, you can use a tear down function. The
* fixture argument is the value returned by the setup function
* above. */
static void
test_compare_tear_down(void* fixture) {
munit_assert_ptr_equal(fixture, (void*)(uintptr_t)0xdeadbeef);
}
static char* foo_params[] = {
(char*) "one", (char*) "two", (char*) "three", NULL
};
static char* bar_params[] = {
(char*) "red", (char*) "green", (char*) "blue", NULL
};
static MunitParameterEnum test_params[] = {
{ (char*) "foo", foo_params },
{ (char*) "bar", bar_params },
{ (char*) "baz", NULL },
{ NULL, NULL },
};
/* Creating a test suite is pretty simple. First, you'll need an
* array of tests: */
static MunitTest test_suite_tests[] = {
{
/* The name is just a unique human-readable way to identify the
* test. You can use it to run a specific test if you want, but
* usually it's mostly decorative. */
(char*) "/example/compare",
/* You probably won't be surprised to learn that the tests are
* functions. */
test_compare,
/* If you want, you can supply a function to set up a fixture. If
* you supply NULL, the user_data parameter from munit_suite_main
* will be used directly. If, however, you provide a callback
* here the user_data parameter will be passed to this callback,
* and the return value from this callback will be passed to the
* test function.
*
* For our example we don't really need a fixture, but lets
* provide one anyways. */
test_compare_setup,
/* If you passed a callback for the fixture setup function, you
* may want to pass a corresponding callback here to reverse the
* operation. */
test_compare_tear_down,
/* Finally, there is a bitmask for options you can pass here. You
* can provide either MUNIT_TEST_OPTION_NONE or 0 here to use the
* defaults. */
MUNIT_TEST_OPTION_NONE,
NULL
},
/* Usually this is written in a much more compact format; all these
* comments kind of ruin that, though. Here is how you'll usually
* see entries written: */
{ (char*) "/example/rand", test_rand, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
/* To tell the test runner when the array is over, just add a NULL
* entry at the end. */
{ (char*) "/example/parameters", test_parameters, NULL, NULL, MUNIT_TEST_OPTION_NONE, test_params },
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
/* If you wanted to have your test suite run other test suites you
* could declare an array of them. Of course each sub-suite can
* contain more suites, etc. */
/* static const MunitSuite other_suites[] = { */
/* { "/second", test_suite_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }, */
/* { NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE } */
/* }; */
/* Now we'll actually declare the test suite. You could do this in
* the main function, or on the heap, or whatever you want. */
static const MunitSuite test_suite = {
/* This string will be prepended to all test names in this suite;
* for example, "/example/rand" will become "/µnit/example/rand".
* Note that, while it doesn't really matter for the top-level
* suite, NULL signal the end of an array of tests; you should use
* an empty string ("") instead. */
(char*) "",
/* The first parameter is the array of test suites. */
test_suite_tests,
/* In addition to containing test cases, suites can contain other
* test suites. This isn't necessary in this example, but it can be
* a great help to projects with lots of tests by making it easier
* to spread the tests across many files. This is where you would
* put "other_suites" (which is commented out above). */
NULL,
/* An interesting feature of µnit is that it supports automatically
* running multiple iterations of the tests. This is usually only
* interesting if you make use of the PRNG to randomize your tests
* cases a bit, or if you are doing performance testing and want to
* average multiple runs. 0 is an alias for 1. */
1,
/* Just like MUNIT_TEST_OPTION_NONE, you can provide
* MUNIT_SUITE_OPTION_NONE or 0 to use the default settings. */
MUNIT_SUITE_OPTION_NONE
};
/* This is only necessary for EXIT_SUCCESS and EXIT_FAILURE, which you
* *should* be using but probably aren't (no, zero and non-zero don't
* always mean success and failure). I guess my point is that nothing
* about µnit requires it. */
#include <stdlib.h>
int main(int argc, char* argv[MUNIT_ARRAY_PARAM(argc + 1)]) {
/* Finally, we'll actually run our test suite! That second argument
* is the user_data parameter which will be passed either to the
* test or (if provided) the fixture setup function. */
return munit_suite_main(&test_suite, (void*) "µnit", argc, argv);
}

View file

@ -1,37 +0,0 @@
project('munit', 'c')
conf_data = configuration_data()
conf_data.set('version', '0.2.0')
add_project_arguments('-std=c99', language : 'c')
cc = meson.get_compiler('c')
root_include = include_directories('.')
munit = library('munit',
['munit.c'],
install: meson.is_subproject())
if meson.is_subproject()
munit_dep = declare_dependency(
include_directories : root_include,
link_with : munit)
else
# standalone install
install_headers('munit.h')
pkg = import('pkgconfig')
pkg.generate(name: 'munit',
description: 'µnit Testing Library for C',
version: conf_data.get('version'),
libraries: munit)
# compile the demo project
munit_example_src = files('example.c')
munit_example = executable('munit_example', munit_example_src,
include_directories: root_include,
link_with: munit)
test('munit example test', munit_example)
endif

File diff suppressed because it is too large Load diff

View file

@ -1,535 +0,0 @@
/* µnit Testing Framework
* Copyright (c) 2013-2017 Evan Nemerson <evan@nemerson.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#if !defined(MUNIT_H)
#define MUNIT_H
#include <stdarg.h>
#include <stdlib.h>
#define MUNIT_VERSION(major, minor, revision) \
(((major) << 16) | ((minor) << 8) | (revision))
#define MUNIT_CURRENT_VERSION MUNIT_VERSION(0, 4, 1)
#if defined(_MSC_VER) && (_MSC_VER < 1600)
# define munit_int8_t __int8
# define munit_uint8_t unsigned __int8
# define munit_int16_t __int16
# define munit_uint16_t unsigned __int16
# define munit_int32_t __int32
# define munit_uint32_t unsigned __int32
# define munit_int64_t __int64
# define munit_uint64_t unsigned __int64
#else
# include <stdint.h>
# define munit_int8_t int8_t
# define munit_uint8_t uint8_t
# define munit_int16_t int16_t
# define munit_uint16_t uint16_t
# define munit_int32_t int32_t
# define munit_uint32_t uint32_t
# define munit_int64_t int64_t
# define munit_uint64_t uint64_t
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1800)
# if !defined(PRIi8)
# define PRIi8 "i"
# endif
# if !defined(PRIi16)
# define PRIi16 "i"
# endif
# if !defined(PRIi32)
# define PRIi32 "i"
# endif
# if !defined(PRIi64)
# define PRIi64 "I64i"
# endif
# if !defined(PRId8)
# define PRId8 "d"
# endif
# if !defined(PRId16)
# define PRId16 "d"
# endif
# if !defined(PRId32)
# define PRId32 "d"
# endif
# if !defined(PRId64)
# define PRId64 "I64d"
# endif
# if !defined(PRIx8)
# define PRIx8 "x"
# endif
# if !defined(PRIx16)
# define PRIx16 "x"
# endif
# if !defined(PRIx32)
# define PRIx32 "x"
# endif
# if !defined(PRIx64)
# define PRIx64 "I64x"
# endif
# if !defined(PRIu8)
# define PRIu8 "u"
# endif
# if !defined(PRIu16)
# define PRIu16 "u"
# endif
# if !defined(PRIu32)
# define PRIu32 "u"
# endif
# if !defined(PRIu64)
# define PRIu64 "I64u"
# endif
# if !defined(bool)
# define bool int
# endif
# if !defined(true)
# define true (!0)
# endif
# if !defined(false)
# define false (!!0)
# endif
#else
# include <inttypes.h>
# include <stdbool.h>
#endif
#if defined(__cplusplus)
extern "C" {
#endif
#if defined(__GNUC__)
# define MUNIT_LIKELY(expr) (__builtin_expect ((expr), 1))
# define MUNIT_UNLIKELY(expr) (__builtin_expect ((expr), 0))
# define MUNIT_UNUSED __attribute__((__unused__))
#else
# define MUNIT_LIKELY(expr) (expr)
# define MUNIT_UNLIKELY(expr) (expr)
# define MUNIT_UNUSED
#endif
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__PGI)
# define MUNIT_ARRAY_PARAM(name) name
#else
# define MUNIT_ARRAY_PARAM(name)
#endif
#if !defined(_WIN32)
# define MUNIT_SIZE_MODIFIER "z"
# define MUNIT_CHAR_MODIFIER "hh"
# define MUNIT_SHORT_MODIFIER "h"
#else
# if defined(_M_X64) || defined(__amd64__)
# define MUNIT_SIZE_MODIFIER "I64"
# else
# define MUNIT_SIZE_MODIFIER ""
# endif
# define MUNIT_CHAR_MODIFIER ""
# define MUNIT_SHORT_MODIFIER ""
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
# define MUNIT_NO_RETURN _Noreturn
#elif defined(__GNUC__)
# define MUNIT_NO_RETURN __attribute__((__noreturn__))
#elif defined(_MSC_VER)
# define MUNIT_NO_RETURN __declspec(noreturn)
#else
# define MUNIT_NO_RETURN
#endif
#if defined(_MSC_VER) && (_MSC_VER >= 1500)
# define MUNIT__PUSH_DISABLE_MSVC_C4127 __pragma(warning(push)) __pragma(warning(disable:4127))
# define MUNIT__POP_DISABLE_MSVC_C4127 __pragma(warning(pop))
#else
# define MUNIT__PUSH_DISABLE_MSVC_C4127
# define MUNIT__POP_DISABLE_MSVC_C4127
#endif
typedef enum {
MUNIT_LOG_DEBUG,
MUNIT_LOG_INFO,
MUNIT_LOG_WARNING,
MUNIT_LOG_ERROR
} MunitLogLevel;
#if defined(__GNUC__) && !defined(__MINGW32__)
# define MUNIT_PRINTF(string_index, first_to_check) __attribute__((format (printf, string_index, first_to_check)))
#else
# define MUNIT_PRINTF(string_index, first_to_check)
#endif
MUNIT_PRINTF(4, 5)
void munit_logf_ex(MunitLogLevel level, const char* filename, int line, const char* format, ...);
#define munit_logf(level, format, ...) \
munit_logf_ex(level, __FILE__, __LINE__, format, __VA_ARGS__)
#define munit_log(level, msg) \
munit_logf(level, "%s", msg)
MUNIT_NO_RETURN
MUNIT_PRINTF(3, 4)
void munit_errorf_ex(const char* filename, int line, const char* format, ...);
#define munit_errorf(format, ...) \
munit_errorf_ex(__FILE__, __LINE__, format, __VA_ARGS__)
#define munit_error(msg) \
munit_errorf("%s", msg)
#define munit_assert(expr) \
do { \
if (!MUNIT_LIKELY(expr)) { \
munit_error("assertion failed: " #expr); \
} \
MUNIT__PUSH_DISABLE_MSVC_C4127 \
} while (0) \
MUNIT__POP_DISABLE_MSVC_C4127
#define munit_assert_true(expr) \
do { \
if (!MUNIT_LIKELY(expr)) { \
munit_error("assertion failed: " #expr " is not true"); \
} \
MUNIT__PUSH_DISABLE_MSVC_C4127 \
} while (0) \
MUNIT__POP_DISABLE_MSVC_C4127
#define munit_assert_false(expr) \
do { \
if (!MUNIT_LIKELY(!(expr))) { \
munit_error("assertion failed: " #expr " is not false"); \
} \
MUNIT__PUSH_DISABLE_MSVC_C4127 \
} while (0) \
MUNIT__POP_DISABLE_MSVC_C4127
#define munit_assert_type_full(prefix, suffix, T, fmt, a, op, b) \
do { \
T munit_tmp_a_ = (a); \
T munit_tmp_b_ = (b); \
if (!(munit_tmp_a_ op munit_tmp_b_)) { \
munit_errorf("assertion failed: %s %s %s (" prefix "%" fmt suffix " %s " prefix "%" fmt suffix ")", \
#a, #op, #b, munit_tmp_a_, #op, munit_tmp_b_); \
} \
MUNIT__PUSH_DISABLE_MSVC_C4127 \
} while (0) \
MUNIT__POP_DISABLE_MSVC_C4127
#define munit_assert_type(T, fmt, a, op, b) \
munit_assert_type_full("", "", T, fmt, a, op, b)
#define munit_assert_char(a, op, b) \
munit_assert_type_full("'\\x", "'", char, "02" MUNIT_CHAR_MODIFIER "x", a, op, b)
#define munit_assert_uchar(a, op, b) \
munit_assert_type_full("'\\x", "'", unsigned char, "02" MUNIT_CHAR_MODIFIER "x", a, op, b)
#define munit_assert_short(a, op, b) \
munit_assert_type(short, MUNIT_SHORT_MODIFIER "d", a, op, b)
#define munit_assert_ushort(a, op, b) \
munit_assert_type(unsigned short, MUNIT_SHORT_MODIFIER "u", a, op, b)
#define munit_assert_int(a, op, b) \
munit_assert_type(int, "d", a, op, b)
#define munit_assert_uint(a, op, b) \
munit_assert_type(unsigned int, "u", a, op, b)
#define munit_assert_long(a, op, b) \
munit_assert_type(long int, "ld", a, op, b)
#define munit_assert_ulong(a, op, b) \
munit_assert_type(unsigned long int, "lu", a, op, b)
#define munit_assert_llong(a, op, b) \
munit_assert_type(long long int, "lld", a, op, b)
#define munit_assert_ullong(a, op, b) \
munit_assert_type(unsigned long long int, "llu", a, op, b)
#define munit_assert_size(a, op, b) \
munit_assert_type(size_t, MUNIT_SIZE_MODIFIER "u", a, op, b)
#define munit_assert_float(a, op, b) \
munit_assert_type(float, "f", a, op, b)
#define munit_assert_double(a, op, b) \
munit_assert_type(double, "g", a, op, b)
#define munit_assert_ptr(a, op, b) \
munit_assert_type(const void*, "p", a, op, b)
#define munit_assert_int8(a, op, b) \
munit_assert_type(munit_int8_t, PRIi8, a, op, b)
#define munit_assert_uint8(a, op, b) \
munit_assert_type(munit_uint8_t, PRIu8, a, op, b)
#define munit_assert_int16(a, op, b) \
munit_assert_type(munit_int16_t, PRIi16, a, op, b)
#define munit_assert_uint16(a, op, b) \
munit_assert_type(munit_uint16_t, PRIu16, a, op, b)
#define munit_assert_int32(a, op, b) \
munit_assert_type(munit_int32_t, PRIi32, a, op, b)
#define munit_assert_uint32(a, op, b) \
munit_assert_type(munit_uint32_t, PRIu32, a, op, b)
#define munit_assert_int64(a, op, b) \
munit_assert_type(munit_int64_t, PRIi64, a, op, b)
#define munit_assert_uint64(a, op, b) \
munit_assert_type(munit_uint64_t, PRIu64, a, op, b)
#define munit_assert_double_equal(a, b, precision) \
do { \
const double munit_tmp_a_ = (a); \
const double munit_tmp_b_ = (b); \
const double munit_tmp_diff_ = ((munit_tmp_a_ - munit_tmp_b_) < 0) ? \
-(munit_tmp_a_ - munit_tmp_b_) : \
(munit_tmp_a_ - munit_tmp_b_); \
if (MUNIT_UNLIKELY(munit_tmp_diff_ > 1e-##precision)) { \
munit_errorf("assertion failed: %s == %s (%0." #precision "g == %0." #precision "g)", \
#a, #b, munit_tmp_a_, munit_tmp_b_); \
} \
MUNIT__PUSH_DISABLE_MSVC_C4127 \
} while (0) \
MUNIT__POP_DISABLE_MSVC_C4127
#include <string.h>
#define munit_assert_string_equal(a, b) \
do { \
const char* munit_tmp_a_ = a; \
const char* munit_tmp_b_ = b; \
if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) != 0)) { \
munit_errorf("assertion failed: string %s == %s (\"%s\" == \"%s\")", \
#a, #b, munit_tmp_a_, munit_tmp_b_); \
} \
MUNIT__PUSH_DISABLE_MSVC_C4127 \
} while (0) \
MUNIT__POP_DISABLE_MSVC_C4127
#define munit_assert_string_not_equal(a, b) \
do { \
const char* munit_tmp_a_ = a; \
const char* munit_tmp_b_ = b; \
if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) == 0)) { \
munit_errorf("assertion failed: string %s != %s (\"%s\" == \"%s\")", \
#a, #b, munit_tmp_a_, munit_tmp_b_); \
} \
MUNIT__PUSH_DISABLE_MSVC_C4127 \
} while (0) \
MUNIT__POP_DISABLE_MSVC_C4127
#define munit_assert_memory_equal(size, a, b) \
do { \
const unsigned char* munit_tmp_a_ = (const unsigned char*) (a); \
const unsigned char* munit_tmp_b_ = (const unsigned char*) (b); \
const size_t munit_tmp_size_ = (size); \
if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) != 0) { \
size_t munit_tmp_pos_; \
for (munit_tmp_pos_ = 0 ; munit_tmp_pos_ < munit_tmp_size_ ; munit_tmp_pos_++) { \
if (munit_tmp_a_[munit_tmp_pos_] != munit_tmp_b_[munit_tmp_pos_]) { \
munit_errorf("assertion failed: memory %s == %s, at offset %" MUNIT_SIZE_MODIFIER "u", \
#a, #b, munit_tmp_pos_); \
break; \
} \
} \
} \
MUNIT__PUSH_DISABLE_MSVC_C4127 \
} while (0) \
MUNIT__POP_DISABLE_MSVC_C4127
#define munit_assert_memory_not_equal(size, a, b) \
do { \
const unsigned char* munit_tmp_a_ = (const unsigned char*) (a); \
const unsigned char* munit_tmp_b_ = (const unsigned char*) (b); \
const size_t munit_tmp_size_ = (size); \
if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) == 0) { \
munit_errorf("assertion failed: memory %s != %s (%zu bytes)", \
#a, #b, munit_tmp_size_); \
} \
MUNIT__PUSH_DISABLE_MSVC_C4127 \
} while (0) \
MUNIT__POP_DISABLE_MSVC_C4127
#define munit_assert_ptr_equal(a, b) \
munit_assert_ptr(a, ==, b)
#define munit_assert_ptr_not_equal(a, b) \
munit_assert_ptr(a, !=, b)
#define munit_assert_null(ptr) \
munit_assert_ptr(ptr, ==, NULL)
#define munit_assert_not_null(ptr) \
munit_assert_ptr(ptr, !=, NULL)
#define munit_assert_ptr_null(ptr) \
munit_assert_ptr(ptr, ==, NULL)
#define munit_assert_ptr_not_null(ptr) \
munit_assert_ptr(ptr, !=, NULL)
/*** Memory allocation ***/
void* munit_malloc_ex(const char* filename, int line, size_t size);
#define munit_malloc(size) \
munit_malloc_ex(__FILE__, __LINE__, (size))
#define munit_new(type) \
((type*) munit_malloc(sizeof(type)))
#define munit_calloc(nmemb, size) \
munit_malloc((nmemb) * (size))
#define munit_newa(type, nmemb) \
((type*) munit_calloc((nmemb), sizeof(type)))
/*** Random number generation ***/
void munit_rand_seed(munit_uint32_t seed);
munit_uint32_t munit_rand_uint32(void);
int munit_rand_int_range(int min, int max);
double munit_rand_double(void);
void munit_rand_memory(size_t size, munit_uint8_t buffer[MUNIT_ARRAY_PARAM(size)]);
/*** Tests and Suites ***/
typedef enum {
/* Test successful */
MUNIT_OK,
/* Test failed */
MUNIT_FAIL,
/* Test was skipped */
MUNIT_SKIP,
/* Test failed due to circumstances not intended to be tested
* (things like network errors, invalid parameter value, failure to
* allocate memory in the test harness, etc.). */
MUNIT_ERROR
} MunitResult;
typedef struct {
char* name;
char** values;
} MunitParameterEnum;
typedef struct {
char* name;
char* value;
} MunitParameter;
const char* munit_parameters_get(const MunitParameter params[], const char* key);
typedef enum {
MUNIT_TEST_OPTION_NONE = 0,
MUNIT_TEST_OPTION_SINGLE_ITERATION = 1 << 0,
MUNIT_TEST_OPTION_TODO = 1 << 1
} MunitTestOptions;
typedef MunitResult (* MunitTestFunc)(const MunitParameter params[], void* user_data_or_fixture);
typedef void* (* MunitTestSetup)(const MunitParameter params[], void* user_data);
typedef void (* MunitTestTearDown)(void* fixture);
typedef struct {
char* name;
MunitTestFunc test;
MunitTestSetup setup;
MunitTestTearDown tear_down;
MunitTestOptions options;
MunitParameterEnum* parameters;
} MunitTest;
typedef enum {
MUNIT_SUITE_OPTION_NONE = 0
} MunitSuiteOptions;
typedef struct MunitSuite_ MunitSuite;
struct MunitSuite_ {
char* prefix;
MunitTest* tests;
MunitSuite* suites;
unsigned int iterations;
MunitSuiteOptions options;
};
int munit_suite_main(const MunitSuite* suite, void* user_data, int argc, char* const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
/* Note: I'm not very happy with this API; it's likely to change if I
* figure out something better. Suggestions welcome. */
typedef struct MunitArgument_ MunitArgument;
struct MunitArgument_ {
char* name;
bool (* parse_argument)(const MunitSuite* suite, void* user_data, int* arg, int argc, char* const argv[MUNIT_ARRAY_PARAM(argc + 1)]);
void (* write_help)(const MunitArgument* argument, void* user_data);
};
int munit_suite_main_custom(const MunitSuite* suite,
void* user_data,
int argc, char* const argv[MUNIT_ARRAY_PARAM(argc + 1)],
const MunitArgument arguments[]);
#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
#define assert_true(expr) munit_assert_true(expr)
#define assert_false(expr) munit_assert_false(expr)
#define assert_char(a, op, b) munit_assert_char(a, op, b)
#define assert_uchar(a, op, b) munit_assert_uchar(a, op, b)
#define assert_short(a, op, b) munit_assert_short(a, op, b)
#define assert_ushort(a, op, b) munit_assert_ushort(a, op, b)
#define assert_int(a, op, b) munit_assert_int(a, op, b)
#define assert_uint(a, op, b) munit_assert_uint(a, op, b)
#define assert_long(a, op, b) munit_assert_long(a, op, b)
#define assert_ulong(a, op, b) munit_assert_ulong(a, op, b)
#define assert_llong(a, op, b) munit_assert_llong(a, op, b)
#define assert_ullong(a, op, b) munit_assert_ullong(a, op, b)
#define assert_size(a, op, b) munit_assert_size(a, op, b)
#define assert_float(a, op, b) munit_assert_float(a, op, b)
#define assert_double(a, op, b) munit_assert_double(a, op, b)
#define assert_ptr(a, op, b) munit_assert_ptr(a, op, b)
#define assert_int8(a, op, b) munit_assert_int8(a, op, b)
#define assert_uint8(a, op, b) munit_assert_uint8(a, op, b)
#define assert_int16(a, op, b) munit_assert_int16(a, op, b)
#define assert_uint16(a, op, b) munit_assert_uint16(a, op, b)
#define assert_int32(a, op, b) munit_assert_int32(a, op, b)
#define assert_uint32(a, op, b) munit_assert_uint32(a, op, b)
#define assert_int64(a, op, b) munit_assert_int64(a, op, b)
#define assert_uint64(a, op, b) munit_assert_uint64(a, op, b)
#define assert_double_equal(a, b, precision) munit_assert_double_equal(a, b, precision)
#define assert_string_equal(a, b) munit_assert_string_equal(a, b)
#define assert_string_not_equal(a, b) munit_assert_string_not_equal(a, b)
#define assert_memory_equal(size, a, b) munit_assert_memory_equal(size, a, b)
#define assert_memory_not_equal(size, a, b) munit_assert_memory_not_equal(size, a, b)
#define assert_ptr_equal(a, b) munit_assert_ptr_equal(a, b)
#define assert_ptr_not_equal(a, b) munit_assert_ptr_not_equal(a, b)
#define assert_ptr_null(ptr) munit_assert_null_equal(ptr)
#define assert_ptr_not_null(ptr) munit_assert_not_null(ptr)
#define assert_null(ptr) munit_assert_null(ptr)
#define assert_not_null(ptr) munit_assert_not_null(ptr)
#endif /* defined(MUNIT_ENABLE_ASSERT_ALIASES) */
#if defined(__cplusplus)
}
#endif
#endif /* !defined(MUNIT_H) */
#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
# if defined(assert)
# undef assert
# endif
# define assert(expr) munit_assert(expr)
#endif

View file

@ -1,40 +1,44 @@
#include <unistd.h>
#include "t.h"
#include "tests.h"
#include "tests_macros.h"
#include "ft_base64.h"
#include "libft.h"
TEST_RESULT should_init_base64_ctx(TEST_PARAMS, TEST_DATA)
static int init_ctx()
{
UNUSED(test_params);
UNUSED(test_data);
t_base64_ctx ctx;
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz0123456789+/";
ft_base64_init(&ctx);
munit_assert_true(ctx.input_fd == STDIN_FILENO);
munit_assert_true(ctx.output_fd == STDOUT_FILENO);
_is(ctx.input_fd == STDIN_FILENO);
_is(ctx.output_fd == STDOUT_FILENO);
munit_assert_string_equal(alphabet, (char *)ctx.alphabet);
_is(ft_strcmp(alphabet, (char *)ctx.alphabet) == 0);
return MUNIT_OK;
_end("init ctx");
}
TEST_RESULT should_transform_base64_block(TEST_PARAMS, TEST_DATA)
static int transform_block()
{
UNUSED(test_params);
UNUSED(test_data);
t_base64_ctx ctx;
t_byte1 buff[FT_BASE64_DECODE_BLOCK_SIZE];
ft_base64_init(&ctx);
ft_base64_encode_transform(&ctx, (t_byte1 *)"Man", buff);
munit_assert_string_equal((char *)buff, "TWFu");
_is(ft_strcmp((char *)buff, "TWFu") == 0);
ft_base64_encode_transform(&ctx, (t_byte1 *)"LOL", buff);
munit_assert_string_equal((char *)buff, "TE9M");
return MUNIT_OK;
_is(ft_strcmp((char *)buff, "TE9M") == 0);
_end("transform block");
}
int base64_tests(void)
{
_should(init_ctx);
_should(transform_block);
return 0;
}

View file

@ -10,35 +10,29 @@
/* */
/* ************************************************************************** */
#include "tests.h"
#include "tests_macros.h"
#include "t.h"
#include "ft_md5.h"
#include "libft.h"
TEST_RESULT should_init_md5_ctx(TEST_PARAMS, TEST_DATA)
static int init_ctx()
{
UNUSED(test_params);
UNUSED(test_data);
t_md5_ctx ctx;
ft_md5_init(&ctx);
munit_assert_uint(ctx.a, ==, 0x67452301);
munit_assert_uint(ctx.b, ==, 0xefcdab89);
munit_assert_uint(ctx.c, ==, 0x98badcfe);
munit_assert_uint(ctx.d, ==, 0x10325476);
munit_assert_true(ctx.bit_len == 0);
for (int i = 0; i < 64; i++)
munit_assert_uchar(ctx.block[i], ==, 0);
_is(ctx.a == 0x67452301);
_is(ctx.b == 0xefcdab89);
_is(ctx.c == 0x98badcfe);
_is(ctx.d == 0x10325476);
_is(ctx.bit_len == 0);
for (int i = 0; i < FT_MD5_BLOCK_SIZE; i++)
_is(ctx.block[i] == 0);
return MUNIT_OK;
_end("init ctx");
}
TEST_RESULT md5_decode_string_to_int(TEST_PARAMS, TEST_DATA)
static int decode_string_to_int()
{
UNUSED(test_params);
UNUSED(test_data);
t_byte1 block[FT_MD5_BLOCK_SIZE];
t_byte4 words[FT_MD5_WORDS_COUNT];
@ -49,17 +43,15 @@ TEST_RESULT md5_decode_string_to_int(TEST_PARAMS, TEST_DATA)
ft_md5_decode(words, block);
munit_assert_true((words[0] & 0xff) == 97);
munit_assert_true(((words[2] >> 8) & 0xff) == 98);
munit_assert_true(((words[15] >> 24) & 0xff) == 99);
_is((words[0] & 0xff) == 97);
_is(((words[2] >> 8) & 0xff) == 98);
_is(((words[15] >> 24) & 0xff) == 99);
return MUNIT_OK;
_end("decode string to int");
}
TEST_RESULT md5_update_change_count(TEST_PARAMS, TEST_DATA)
static int update_change_count()
{
UNUSED(test_params);
UNUSED(test_data);
t_md5_ctx ctx;
char message[] = "hello, World!";
t_byte8 size = ft_strlen(message);
@ -67,16 +59,13 @@ TEST_RESULT md5_update_change_count(TEST_PARAMS, TEST_DATA)
ft_md5_init(&ctx);
ft_md5_update(&ctx, (t_byte1 *)message, size);
munit_assert_true(size * 8 == ctx.bit_len);
_is(size * 8 == ctx.bit_len);
return MUNIT_OK;
_end("update change count");
}
TEST_RESULT md5_encode_bits_to_string(TEST_PARAMS, TEST_DATA)
static int encode_bits_to_string()
{
UNUSED(test_params);
UNUSED(test_data);
t_byte8 len;
t_byte1 bits[FT_MD5_MESSAGE_LENGTH_BYTE];
@ -94,18 +83,15 @@ TEST_RESULT md5_encode_bits_to_string(TEST_PARAMS, TEST_DATA)
ft_md5_encode_len(bits, len);
munit_assert_true(bits[7] == (t_byte1)((len >> 56) & 0xff));
munit_assert_true(bits[0] == (t_byte1)(len & 0xff));
munit_assert_true(bits[1] == (t_byte1)((len >> 8) & 0xff));
_is(bits[7] == (t_byte1)((len >> 56) & 0xff));
_is(bits[0] == (t_byte1)(len & 0xff));
_is(bits[1] == (t_byte1)((len >> 8) & 0xff));
return MUNIT_OK;
_end("encode bits to string");
}
TEST_RESULT md5_encode_register(TEST_PARAMS, TEST_DATA)
static int encode_register()
{
UNUSED(test_params);
UNUSED(test_data);
t_byte1 digest_part[4];
t_byte4 reg;
@ -119,19 +105,16 @@ TEST_RESULT md5_encode_register(TEST_PARAMS, TEST_DATA)
ft_md5_encode_register(digest_part, reg);
munit_assert_true(digest_part[0] == (t_byte1)(reg & 0xff));
munit_assert_true(digest_part[1] == (t_byte1)((reg >> 8) & 0xff));
munit_assert_true(digest_part[2] == (t_byte1)((reg >> 16) & 0xff));
munit_assert_true(digest_part[3] == (t_byte1)((reg >> 24) & 0xff));
_is(digest_part[0] == (t_byte1)(reg & 0xff));
_is(digest_part[1] == (t_byte1)((reg >> 8) & 0xff));
_is(digest_part[2] == (t_byte1)((reg >> 16) & 0xff));
_is(digest_part[3] == (t_byte1)((reg >> 24) & 0xff));
return MUNIT_OK;
_end("encode register");
}
TEST_RESULT md5_create_digest(TEST_PARAMS, TEST_DATA)
static int create_digest()
{
UNUSED(test_params);
UNUSED(test_data);
t_md5_ctx ctx;
t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE];
t_byte1 digest_string[FT_MD5_STRING_SIZE_BYTE];
@ -146,33 +129,30 @@ TEST_RESULT md5_create_digest(TEST_PARAMS, TEST_DATA)
ft_md5_final(digest, &ctx);
ft_md5_digest_string(digest, digest_string);
munit_assert_string_equal((const char *)digest_string,
"d41d8cd98f00b204e9800998ecf8427e");
_is(ft_strcmp((const char *)digest_string,
"d41d8cd98f00b204e9800998ecf8427e") == 0);
ft_md5_init(&ctx);
ft_md5_update(&ctx, case2, ft_strlen((const char *)case2));
ft_md5_final(digest, &ctx);
ft_md5_digest_string(digest, digest_string);
munit_assert_string_equal((const char *)digest_string,
"0cc175b9c0f1b6a831c399e269772661");
_is(ft_strcmp((const char *)digest_string,
"0cc175b9c0f1b6a831c399e269772661") == 0);
ft_md5_init(&ctx);
ft_md5_update(&ctx, case7, ft_strlen((const char *)case7));
ft_md5_final(digest, &ctx);
ft_md5_digest_string(digest, digest_string);
munit_assert_string_equal((const char *)digest_string,
"2580a0aff7ef5e80f6b5432666530926");
_is(ft_strcmp((const char *)digest_string,
"2580a0aff7ef5e80f6b5432666530926") == 0);
return MUNIT_OK;
_end("create digest");
}
TEST_RESULT md5_create_string(TEST_PARAMS, TEST_DATA)
static int create_digest_string()
{
UNUSED(test_params);
UNUSED(test_data);
t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE];
t_byte1 digest_string[FT_MD5_STRING_SIZE_BYTE];
t_byte4 a;
@ -193,7 +173,20 @@ TEST_RESULT md5_create_string(TEST_PARAMS, TEST_DATA)
ft_md5_digest_string(digest, digest_string);
munit_assert_string_equal((const char *)digest_string, "d41d8cd98f00b204e9800998ecf8427e");
_is(ft_strcmp((const char *)digest_string,
"d41d8cd98f00b204e9800998ecf8427e") == 0);
return MUNIT_OK;
_end("create digest string");
}
int md5_tests()
{
_should(init_ctx);
_should(decode_string_to_int);
_should(update_change_count);
_should(encode_bits_to_string);
_should(encode_register);
_should(create_digest);
_should(create_digest_string);
return 0;
}

View file

@ -10,62 +10,54 @@
/* */
/* ************************************************************************** */
#include "tests.h"
#include "tests_macros.h"
#include "t.h"
#include "ft_sha.h"
#include "libft.h"
TEST_RESULT should_init_ctx_sha256(TEST_PARAMS, TEST_DATA)
static int init_sha256_ctx()
{
UNUSED(test_params);
UNUSED(test_data);
t_sha256_ctx ctx;
ft_sha256_init(&ctx);
munit_assert_uint(ctx.a, ==, 0x6a09e667);
munit_assert_uint(ctx.b, ==, 0xbb67ae85);
munit_assert_uint(ctx.c, ==, 0x3c6ef372);
munit_assert_uint(ctx.d, ==, 0xa54ff53a);
munit_assert_uint(ctx.e, ==, 0x510e527f);
munit_assert_uint(ctx.f, ==, 0x9b05688c);
munit_assert_uint(ctx.g, ==, 0x1f83d9ab);
munit_assert_uint(ctx.h, ==, 0x5be0cd19);
munit_assert_true(ctx.bit_len == 0);
_is(ctx.a == 0x6a09e667);
_is(ctx.b == 0xbb67ae85);
_is(ctx.c == 0x3c6ef372);
_is(ctx.d == 0xa54ff53a);
_is(ctx.e == 0x510e527f);
_is(ctx.f == 0x9b05688c);
_is(ctx.g == 0x1f83d9ab);
_is(ctx.h == 0x5be0cd19);
_is(ctx.bit_len == 0);
for (int i = 0; i < 64; i++)
munit_assert_uchar(ctx.block[i], ==, 0);
_is(ctx.block[i] == 0);
return MUNIT_OK;
_end("init sha256 ctx");
}
TEST_RESULT should_init_ctx_sha224(TEST_PARAMS, TEST_DATA)
static int init_sha224_ctx()
{
UNUSED(test_data);
UNUSED(test_params);
t_sha256_ctx ctx;
ft_sha224_init(&ctx);
munit_assert_uint(ctx.a, ==, 0xc1059ed8);
munit_assert_uint(ctx.b, ==, 0x367cd507);
munit_assert_uint(ctx.c, ==, 0x3070dd17);
munit_assert_uint(ctx.d, ==, 0xf70e5939);
munit_assert_uint(ctx.e, ==, 0xffc00b31);
munit_assert_uint(ctx.f, ==, 0x68581511);
munit_assert_uint(ctx.g, ==, 0x64f98fa7);
munit_assert_uint(ctx.h, ==, 0xbefa4fa4);
munit_assert_true(ctx.bit_len == 0);
_is(ctx.a == 0xc1059ed8);
_is(ctx.b == 0x367cd507);
_is(ctx.c == 0x3070dd17);
_is(ctx.d == 0xf70e5939);
_is(ctx.e == 0xffc00b31);
_is(ctx.f == 0x68581511);
_is(ctx.g == 0x64f98fa7);
_is(ctx.h == 0xbefa4fa4);
_is(ctx.bit_len == 0);
for (int i = 0; i < 64; i++)
munit_assert_uchar(ctx.block[i], ==, 0);
_is(ctx.block[i] == 0);
return MUNIT_OK;
_end("init sha224 ctx");
}
TEST_RESULT decode_string_to_int_sha256(TEST_PARAMS, TEST_DATA)
static int decode_string_to_int_sha256()
{
UNUSED(test_params);
UNUSED(test_data);
t_byte1 block[FT_SHA256_BLOCK_SIZE];
t_byte4 words[FT_SHA256_WORDS_COUNT];
@ -76,16 +68,13 @@ TEST_RESULT decode_string_to_int_sha256(TEST_PARAMS, TEST_DATA)
ft_sha256_decode(words, block);
munit_assert_uint(words[0], ==, 0x61626300);
_is(words[0] == 0x61626300);
return MUNIT_OK;
_end("decode string to int sha256");
}
TEST_RESULT encode_len_to_string_sha256(TEST_PARAMS, TEST_DATA)
static int encode_len_to_string_sha256()
{
UNUSED(test_params);
UNUSED(test_data);
t_byte8 len;
t_byte1 bits[FT_SHA256_MESSAGE_LENGTH_BYTE];
@ -103,18 +92,15 @@ TEST_RESULT encode_len_to_string_sha256(TEST_PARAMS, TEST_DATA)
ft_sha256_encode_len(bits, len);
munit_assert_uchar(bits[0], ==, (t_byte1)((len >> 56) & 0xff));
munit_assert_uchar(bits[7], ==, (t_byte1)(len & 0xff));
munit_assert_uchar(bits[6], ==, (t_byte1)((len >> 8) & 0xff));
_is(bits[0] == (t_byte1)((len >> 56) & 0xff));
_is(bits[7] == (t_byte1)(len & 0xff));
_is(bits[6] == (t_byte1)((len >> 8) & 0xff));
return MUNIT_OK;
_end("encode len to string sha256");
}
TEST_RESULT encode_register_to_string_sha256(TEST_PARAMS, TEST_DATA)
static int encode_register_to_string_sha256()
{
UNUSED(test_params);
UNUSED(test_data);
t_byte1 digest_part[4];
t_byte4 reg;
@ -122,44 +108,38 @@ TEST_RESULT encode_register_to_string_sha256(TEST_PARAMS, TEST_DATA)
ft_sha256_encode_register(digest_part, reg);
munit_assert_uchar(digest_part[0], ==, 0xba);
munit_assert_uchar(digest_part[1], ==, 0x78);
munit_assert_uchar(digest_part[2], ==, 0x16);
munit_assert_uchar(digest_part[3], ==, 0xbf);
_is(digest_part[0] == 0xba);
_is(digest_part[1] == 0x78);
_is(digest_part[2] == 0x16);
_is(digest_part[3] == 0xbf);
return MUNIT_OK;
_end("encode register to string sha256");
}
TEST_RESULT update_bit_count_sha256(TEST_PARAMS, TEST_DATA)
static int update_bit_count_sha256()
{
UNUSED(test_data);
UNUSED(test_params);
t_sha256_ctx ctx;
t_byte1 message[] = "abc";
ft_sha256_init(&ctx);
ft_sha256_update(&ctx, message, sizeof(message));
munit_assert_uint(ctx.bit_len, ==, sizeof(message) * 8);
_is(ctx.bit_len == sizeof(message) * 8);
return MUNIT_OK;
_end("update bit count sha256");
}
TEST_RESULT fill_buffer_sha256(TEST_PARAMS, TEST_DATA)
static int fill_buffer_sha256()
{
UNUSED(test_data);
UNUSED(test_params);
t_sha256_ctx ctx;
t_byte1 message[] = "abc";
ft_sha256_init(&ctx);
ft_sha256_update(&ctx, message, sizeof(message));
munit_assert_string_equal((const char *)message, (const char *)ctx.block);
_is(ft_strcmp((const char *)message, (const char *)ctx.block) == 0);
return MUNIT_OK;
_end("fill buffer sha256");
}
static void block_with_right_padding
@ -174,17 +154,15 @@ static void block_with_right_padding
padding[message_len] = 0x80;
}
TEST_RESULT add_right_padding_sha256(TEST_PARAMS, TEST_DATA)
static int add_right_padding_sha256()
{
UNUSED(test_data);
UNUSED(test_params);
t_sha256_ctx ctx;
t_byte1 message[] = "abc";
t_byte8 buff_index;
t_byte8 padding_len;
t_byte1 padding[FT_SHA256_BLOCK_SIZE];
t_byte1 block_with_message_and_pading[FT_SHA256_BLOCK_SIZE];
int i;
ft_sha256_init(&ctx);
ft_sha256_update(&ctx, message, sizeof(message));
@ -195,17 +173,17 @@ TEST_RESULT add_right_padding_sha256(TEST_PARAMS, TEST_DATA)
block_with_right_padding(message, sizeof(message),
block_with_message_and_pading);
munit_assert_memory_equal(FT_SHA256_BLOCK_SIZE, ctx.block,
block_with_message_and_pading);
return MUNIT_OK;
i = 0;
_is(ft_memcmp(ctx.block, block_with_message_and_pading,
FT_SHA256_BLOCK_SIZE) == 0);
_end("add right padding sha256");
}
TEST_RESULT compute_digest_sha256(TEST_PARAMS, TEST_DATA)
static int compute_digest_sha256()
{
UNUSED(test_data);
UNUSED(test_params);
t_byte1 message[] = "abc";
t_sha256_ctx ctx;
t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
@ -214,24 +192,20 @@ TEST_RESULT compute_digest_sha256(TEST_PARAMS, TEST_DATA)
ft_sha256_update(&ctx, message, ft_strlen((const char *)message));
ft_sha256_final(digest, &ctx);
_is(ctx.a == 0xba7816bf);
_is(ctx.b == 0x8f01cfea);
_is(ctx.c == 0x414140de);
_is(ctx.d == 0x5dae2223);
_is(ctx.e == 0xb00361a3);
_is(ctx.f == 0x96177a9c);
_is(ctx.g == 0xb410ff61);
_is(ctx.h == 0xf20015ad);
munit_assert_uint32(ctx.a, ==, 0xba7816bf);
munit_assert_uint32(ctx.b, ==, 0x8f01cfea);
munit_assert_uint32(ctx.c, ==, 0x414140de);
munit_assert_uint32(ctx.d, ==, 0x5dae2223);
munit_assert_uint32(ctx.e, ==, 0xb00361a3);
munit_assert_uint32(ctx.f, ==, 0x96177a9c);
munit_assert_uint32(ctx.g, ==, 0xb410ff61);
munit_assert_uint32(ctx.h, ==, 0xf20015ad);
return MUNIT_OK;
_end("compute digest sha256");
}
TEST_RESULT compute_digest_sha224(TEST_PARAMS, TEST_DATA)
static int compute_digest_sha224()
{
UNUSED(test_data);
UNUSED(test_params);
t_byte1 message[] = "abc";
t_sha256_ctx ctx;
t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
@ -240,22 +214,19 @@ TEST_RESULT compute_digest_sha224(TEST_PARAMS, TEST_DATA)
ft_sha224_update(&ctx, message, ft_strlen((const char *)message));
ft_sha224_final(digest, &ctx);
munit_assert_uint32(ctx.a, ==, 0x23097d22);
munit_assert_uint32(ctx.b, ==, 0x3405d822);
munit_assert_uint32(ctx.c, ==, 0x8642a477);
munit_assert_uint32(ctx.d, ==, 0xbda255b3);
munit_assert_uint32(ctx.e, ==, 0x2aadbce4);
munit_assert_uint32(ctx.f, ==, 0xbda0b3f7);
munit_assert_uint32(ctx.g, ==, 0xe36c9da7);
_is(ctx.a == 0x23097d22);
_is(ctx.b == 0x3405d822);
_is(ctx.c == 0x8642a477);
_is(ctx.d == 0xbda255b3);
_is(ctx.e == 0x2aadbce4);
_is(ctx.f == 0xbda0b3f7);
_is(ctx.g == 0xe36c9da7);
return MUNIT_OK;
_end("computee digst sha224");
}
TEST_RESULT create_digest_string_sha256(TEST_PARAMS, TEST_DATA)
static int create_digest_string_sha256()
{
UNUSED(test_data);
UNUSED(test_params);
t_byte1 message[] = "abc";
t_byte1 message_digest[] =
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
@ -268,17 +239,14 @@ TEST_RESULT create_digest_string_sha256(TEST_PARAMS, TEST_DATA)
ft_sha256_final(digest, &ctx);
ft_sha256_digest_string(digest, digest_string);
munit_assert_string_equal((const char *)message_digest,
(const char *)digest_string);
_is(ft_strcmp((const char *)message_digest,
(const char *)digest_string) == 0);
return MUNIT_OK;
_end("create digest string sha256");
}
TEST_RESULT create_digest_string_sha224(TEST_PARAMS, TEST_DATA)
static int create_digest_string_sha224()
{
UNUSED(test_data);
UNUSED(test_params);
t_byte1 message[] = "abc";
t_byte1 message_digest[] =
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
@ -291,8 +259,26 @@ TEST_RESULT create_digest_string_sha224(TEST_PARAMS, TEST_DATA)
ft_sha224_final(digest, &ctx);
ft_sha224_digest_string(digest, digest_string);
munit_assert_string_equal((const char *)message_digest,
(const char *)digest_string);
_is(ft_strcmp((const char *)message_digest,
(const char *)digest_string) == 0);
return MUNIT_OK;
_end("create digest strinf sha224");
}
int sha_tests()
{
_should(init_sha256_ctx);
_should(init_sha224_ctx);
_should(decode_string_to_int_sha256);
_should(encode_len_to_string_sha256);
_should(encode_register_to_string_sha256);
_should(update_bit_count_sha256);
_should(fill_buffer_sha256);
_should(add_right_padding_sha256);
_should(compute_digest_sha256);
_should(compute_digest_sha224);
_should(create_digest_string_sha256);
_should(create_digest_string_sha224);
return 0;
}

View file

@ -10,58 +10,24 @@
/* */
/* ************************************************************************** */
#include "t.h"
#include "tests.h"
#include "tests_macros.h"
MunitTest g_md5_tests[] = {
IT("/init_ctx", should_init_md5_ctx, NULL, NULL, 0, NULL),
IT("/updates_ctx_count", md5_update_change_count, NULL, NULL, 0, NULL),
IT("/decode_string_to_int", md5_decode_string_to_int, NULL, NULL, 0, NULL),
IT("/encode_to_string", md5_encode_bits_to_string, NULL, NULL, 0, NULL),
IT("/encode_register", md5_encode_register, NULL, NULL, 0, NULL),
IT("/creates_digest", md5_create_digest, NULL, NULL, 0, NULL),
IT("/creates_string", md5_create_string, NULL, NULL, 0, NULL),
END_IT
};
MunitTest g_sha_tests[] = {
IT("/init_ctx_256", should_init_ctx_sha256, NULL, NULL, 0, NULL),
IT("/init_ctx_224", should_init_ctx_sha224, NULL, NULL, 0, NULL),
IT("/decode_string", decode_string_to_int_sha256, NULL, NULL, 0, NULL),
IT("/encode_len", encode_len_to_string_sha256, NULL, NULL, 0, NULL),
IT("/enc_register", encode_register_to_string_sha256, NULL, NULL, 0, NULL),
IT("/update_bit_count", update_bit_count_sha256, NULL, NULL, 0, NULL),
IT("/fill_buffer", fill_buffer_sha256, NULL, NULL, 0, NULL),
IT("/add_right_padding", add_right_padding_sha256, NULL, NULL, 0, NULL),
IT("/compute_digest_256", compute_digest_sha256, NULL, NULL, 0, NULL),
IT("/compute_digest_224", compute_digest_sha224, NULL, NULL, 0, NULL),
IT("/creates_string_256", create_digest_string_sha256, NULL, NULL, 0, NULL),
IT("/creates_string_224", create_digest_string_sha224, NULL, NULL, 0, NULL),
END_IT
};
MunitTest g_base64_tests[] = {
IT("/init_ctx", should_init_base64_ctx, NULL, NULL, 0, NULL),
IT("/transform_block", should_transform_base64_block, NULL, NULL, 0, NULL),
END_IT
};
static const MunitSuite g_ft_ssl_suites[] = {
{(char *)"/md5_suite", g_md5_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE},
{(char *)"/sha_suite", g_sha_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE},
{(char *)"/base64_suite", g_base64_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE},
{NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE}
};
static const MunitSuite g_main_suite = {
(char *)"/ft_ssl",
NULL,
(MunitSuite *)g_ft_ssl_suites,
1,
MUNIT_SUITE_OPTION_NONE
};
int main(int argc, char **argv)
int all_tests()
{
return (munit_suite_main(&g_main_suite, NULL, argc, argv));
_verify("md5:", md5_tests);
_verify("sha:", sha_tests);
_verify("base64:", base64_tests);
return 0;
}
int main()
{
int result;
result = all_tests();
if (result == 0)
printf("PASSED\n");
return result != 0;
}