test/test_utils.cpp

Tue, 20 Dec 2022 14:12:36 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 20 Dec 2022 14:12:36 +0100
changeset 633
63a692642aa2
child 646
dfd0403ff8b6
permissions
-rw-r--r--

add utils tests

universe@633 1 /*
universe@633 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@633 3 *
universe@633 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@633 5 *
universe@633 6 * Redistribution and use in source and binary forms, with or without
universe@633 7 * modification, are permitted provided that the following conditions are met:
universe@633 8 *
universe@633 9 * 1. Redistributions of source code must retain the above copyright
universe@633 10 * notice, this list of conditions and the following disclaimer.
universe@633 11 *
universe@633 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@633 13 * notice, this list of conditions and the following disclaimer in the
universe@633 14 * documentation and/or other materials provided with the distribution.
universe@633 15 *
universe@633 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@633 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@633 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@633 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@633 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@633 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@633 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@633 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@633 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@633 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@633 26 * POSSIBILITY OF SUCH DAMAGE.
universe@633 27 */
universe@633 28
universe@633 29 #include "cx/utils.h"
universe@633 30
universe@633 31 #include <gtest/gtest.h>
universe@633 32
universe@633 33 TEST(Utils, ForN) {
universe@633 34 unsigned j;
universe@633 35 j = 0;
universe@633 36 cx_for_n(i, 50) {
universe@633 37 EXPECT_EQ(i, j);
universe@633 38 j++;
universe@633 39 }
universe@633 40 }
universe@633 41
universe@633 42 TEST(Utils, szmul) {
universe@633 43 size_t r;
universe@633 44 int e;
universe@633 45 e = cx_szmul(5, 7, &r);
universe@633 46 EXPECT_EQ(0, e);
universe@633 47 EXPECT_EQ(35, r);
universe@633 48
universe@633 49 size_t s = SIZE_MAX & ~3;
universe@633 50
universe@633 51 e = cx_szmul(s / 4, 2, &r);
universe@633 52 EXPECT_EQ(0, e);
universe@633 53 EXPECT_EQ(s / 2, r);
universe@633 54 e = cx_szmul(2, s / 4, &r);
universe@633 55 EXPECT_EQ(0, e);
universe@633 56 EXPECT_EQ(s / 2, r);
universe@633 57
universe@633 58 e = cx_szmul(s / 4, 4, &r);
universe@633 59 EXPECT_EQ(0, e);
universe@633 60 EXPECT_EQ(s, r);
universe@633 61
universe@633 62 e = cx_szmul(4, s / 4, &r);
universe@633 63 EXPECT_EQ(0, e);
universe@633 64 EXPECT_EQ(s, r);
universe@633 65
universe@633 66 e = cx_szmul(s / 4, 5, &r);
universe@633 67 EXPECT_NE(0, e);
universe@633 68
universe@633 69 e = cx_szmul(5, s / 4, &r);
universe@633 70 EXPECT_NE(0, e);
universe@633 71
universe@633 72 e = cx_szmul(SIZE_MAX - 4, 0, &r);
universe@633 73 EXPECT_EQ(0, e);
universe@633 74 EXPECT_EQ(0, r);
universe@633 75
universe@633 76 e = cx_szmul(0, SIZE_MAX - 1, &r);
universe@633 77 EXPECT_EQ(0, e);
universe@633 78 EXPECT_EQ(0, r);
universe@633 79
universe@633 80 e = cx_szmul(SIZE_MAX, 0, &r);
universe@633 81 EXPECT_EQ(0, e);
universe@633 82 EXPECT_EQ(0, r);
universe@633 83
universe@633 84 e = cx_szmul(0, SIZE_MAX, &r);
universe@633 85 EXPECT_EQ(0, e);
universe@633 86 EXPECT_EQ(0, r);
universe@633 87
universe@633 88 e = cx_szmul(0, 0, &r);
universe@633 89 EXPECT_EQ(0, e);
universe@633 90 EXPECT_EQ(0, r);
universe@633 91 }
universe@633 92
universe@633 93 #ifdef CX_SZMUL_BUILTIN
universe@633 94
universe@633 95 // also test the custom implementation
universe@633 96 struct Utils_szmul_impl : ::testing::Test {
universe@633 97 #undef CX_SZMUL_BUILTIN
universe@633 98
universe@633 99 #include "../src/utils.c"
universe@633 100
universe@633 101 #define CX_SZMUL_BUILTIN
universe@633 102 };
universe@633 103
universe@633 104 TEST_F(Utils_szmul_impl, Test) {
universe@633 105 size_t r;
universe@633 106 int e;
universe@633 107 e = cx_szmul_impl(5, 7, &r);
universe@633 108 EXPECT_EQ(0, e);
universe@633 109 EXPECT_EQ(35, r);
universe@633 110
universe@633 111 size_t s = SIZE_MAX & ~3;
universe@633 112
universe@633 113 e = cx_szmul_impl(s / 4, 2, &r);
universe@633 114 EXPECT_EQ(0, e);
universe@633 115 EXPECT_EQ(s / 2, r);
universe@633 116 e = cx_szmul_impl(2, s / 4, &r);
universe@633 117 EXPECT_EQ(0, e);
universe@633 118 EXPECT_EQ(s / 2, r);
universe@633 119
universe@633 120 e = cx_szmul_impl(s / 4, 4, &r);
universe@633 121 EXPECT_EQ(0, e);
universe@633 122 EXPECT_EQ(s, r);
universe@633 123
universe@633 124 e = cx_szmul_impl(4, s / 4, &r);
universe@633 125 EXPECT_EQ(0, e);
universe@633 126 EXPECT_EQ(s, r);
universe@633 127
universe@633 128 e = cx_szmul_impl(s / 4, 5, &r);
universe@633 129 EXPECT_NE(0, e);
universe@633 130
universe@633 131 e = cx_szmul_impl(5, s / 4, &r);
universe@633 132 EXPECT_NE(0, e);
universe@633 133
universe@633 134 e = cx_szmul_impl(SIZE_MAX - 4, 0, &r);
universe@633 135 EXPECT_EQ(0, e);
universe@633 136 EXPECT_EQ(0, r);
universe@633 137
universe@633 138 e = cx_szmul_impl(0, SIZE_MAX - 1, &r);
universe@633 139 EXPECT_EQ(0, e);
universe@633 140 EXPECT_EQ(0, r);
universe@633 141
universe@633 142 e = cx_szmul_impl(SIZE_MAX, 0, &r);
universe@633 143 EXPECT_EQ(0, e);
universe@633 144 EXPECT_EQ(0, r);
universe@633 145
universe@633 146 e = cx_szmul_impl(0, SIZE_MAX, &r);
universe@633 147 EXPECT_EQ(0, e);
universe@633 148 EXPECT_EQ(0, r);
universe@633 149
universe@633 150 e = cx_szmul_impl(0, 0, &r);
universe@633 151 EXPECT_EQ(0, e);
universe@633 152 EXPECT_EQ(0, r);
universe@633 153 }
universe@633 154
universe@633 155 #endif // CX_SZMUL_BUILTIN

mercurial