Sun, 09 Apr 2023 19:03:58 +0200
refactoring of collections to make use of destructors in map implementations
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@674 | 31 | #include "cx/buffer.h" |
universe@674 | 32 | |
universe@633 | 33 | #include <gtest/gtest.h> |
universe@633 | 34 | |
universe@674 | 35 | TEST(Utils, cx_stream_bncopy) { |
universe@674 | 36 | CxBuffer source, target; |
universe@674 | 37 | char sbuf[32], tbuf[32]; |
universe@674 | 38 | memset(tbuf, 0, 32); |
universe@674 | 39 | cxBufferInit(&source, sbuf, 32, nullptr, 0); |
universe@674 | 40 | cxBufferInit(&target, tbuf, 32, nullptr, 0); |
universe@674 | 41 | cxBufferPutString(&source, "This is a stream copy test."); |
universe@674 | 42 | cxBufferSeek(&source, 0, SEEK_SET); |
universe@674 | 43 | |
universe@674 | 44 | char tmp[4]; |
universe@674 | 45 | size_t result = cx_stream_bncopy(&source, &target, |
universe@674 | 46 | (cx_read_func) cxBufferRead, |
universe@674 | 47 | (cx_write_func) cxBufferWrite, |
universe@674 | 48 | tmp, 4, 20); |
universe@674 | 49 | EXPECT_EQ(20, result); |
universe@674 | 50 | EXPECT_EQ(20, target.size); |
universe@674 | 51 | EXPECT_STREQ("This is a stream cop\0", tbuf); |
universe@674 | 52 | |
universe@674 | 53 | result = cx_stream_bcopy(&source, &target, |
universe@674 | 54 | (cx_read_func) cxBufferRead, |
universe@674 | 55 | (cx_write_func) cxBufferWrite, |
universe@674 | 56 | nullptr, 16); |
universe@674 | 57 | |
universe@674 | 58 | EXPECT_EQ(7, result); |
universe@674 | 59 | EXPECT_EQ(27, target.size); |
universe@674 | 60 | EXPECT_STREQ("This is a stream copy test.\0", tbuf); |
universe@674 | 61 | |
universe@674 | 62 | cxBufferDestroy(&source); |
universe@674 | 63 | cxBufferDestroy(&target); |
universe@674 | 64 | } |
universe@674 | 65 | |
universe@674 | 66 | TEST(Utils, cx_stream_ncopy) { |
universe@674 | 67 | CxBuffer source, target; |
universe@674 | 68 | char sbuf[32], tbuf[32]; |
universe@674 | 69 | memset(tbuf, 0, 32); |
universe@674 | 70 | cxBufferInit(&source, sbuf, 32, nullptr, 0); |
universe@674 | 71 | cxBufferInit(&target, tbuf, 32, nullptr, 0); |
universe@674 | 72 | cxBufferPutString(&source, "This is a stream copy test."); |
universe@674 | 73 | cxBufferSeek(&source, 0, SEEK_SET); |
universe@674 | 74 | |
universe@674 | 75 | size_t result = cx_stream_ncopy(&source, &target, |
universe@674 | 76 | (cx_read_func) cxBufferRead, |
universe@674 | 77 | (cx_write_func) cxBufferWrite, |
universe@674 | 78 | 20); |
universe@674 | 79 | EXPECT_EQ(20, result); |
universe@674 | 80 | EXPECT_EQ(20, target.size); |
universe@674 | 81 | EXPECT_STREQ("This is a stream cop\0", tbuf); |
universe@674 | 82 | |
universe@674 | 83 | result = cx_stream_copy(&source, &target, |
universe@674 | 84 | (cx_read_func) cxBufferRead, |
universe@674 | 85 | (cx_write_func) cxBufferWrite); |
universe@674 | 86 | |
universe@674 | 87 | EXPECT_EQ(7, result); |
universe@674 | 88 | EXPECT_EQ(27, target.size); |
universe@674 | 89 | EXPECT_STREQ("This is a stream copy test.\0", tbuf); |
universe@674 | 90 | |
universe@674 | 91 | cxBufferDestroy(&source); |
universe@674 | 92 | cxBufferDestroy(&target); |
universe@674 | 93 | } |
universe@674 | 94 | |
universe@633 | 95 | TEST(Utils, ForN) { |
universe@633 | 96 | unsigned j; |
universe@633 | 97 | j = 0; |
universe@633 | 98 | cx_for_n(i, 50) { |
universe@633 | 99 | EXPECT_EQ(i, j); |
universe@633 | 100 | j++; |
universe@633 | 101 | } |
universe@633 | 102 | } |
universe@633 | 103 | |
universe@646 | 104 | TEST(Utils, swap_ptr) { |
universe@646 | 105 | int i = 5; |
universe@646 | 106 | int j = 8; |
universe@646 | 107 | int *ip = &i; |
universe@646 | 108 | int *jp = &j; |
universe@646 | 109 | cx_swap_ptr(ip, jp); |
universe@646 | 110 | EXPECT_EQ(ip, &j); |
universe@646 | 111 | EXPECT_EQ(jp, &i); |
universe@646 | 112 | } |
universe@646 | 113 | |
universe@633 | 114 | TEST(Utils, szmul) { |
universe@633 | 115 | size_t r; |
universe@633 | 116 | int e; |
universe@633 | 117 | e = cx_szmul(5, 7, &r); |
universe@633 | 118 | EXPECT_EQ(0, e); |
universe@633 | 119 | EXPECT_EQ(35, r); |
universe@633 | 120 | |
universe@633 | 121 | size_t s = SIZE_MAX & ~3; |
universe@633 | 122 | |
universe@633 | 123 | e = cx_szmul(s / 4, 2, &r); |
universe@633 | 124 | EXPECT_EQ(0, e); |
universe@633 | 125 | EXPECT_EQ(s / 2, r); |
universe@633 | 126 | e = cx_szmul(2, s / 4, &r); |
universe@633 | 127 | EXPECT_EQ(0, e); |
universe@633 | 128 | EXPECT_EQ(s / 2, r); |
universe@633 | 129 | |
universe@633 | 130 | e = cx_szmul(s / 4, 4, &r); |
universe@633 | 131 | EXPECT_EQ(0, e); |
universe@633 | 132 | EXPECT_EQ(s, r); |
universe@633 | 133 | |
universe@633 | 134 | e = cx_szmul(4, s / 4, &r); |
universe@633 | 135 | EXPECT_EQ(0, e); |
universe@633 | 136 | EXPECT_EQ(s, r); |
universe@633 | 137 | |
universe@633 | 138 | e = cx_szmul(s / 4, 5, &r); |
universe@633 | 139 | EXPECT_NE(0, e); |
universe@633 | 140 | |
universe@633 | 141 | e = cx_szmul(5, s / 4, &r); |
universe@633 | 142 | EXPECT_NE(0, e); |
universe@633 | 143 | |
universe@633 | 144 | e = cx_szmul(SIZE_MAX - 4, 0, &r); |
universe@633 | 145 | EXPECT_EQ(0, e); |
universe@633 | 146 | EXPECT_EQ(0, r); |
universe@633 | 147 | |
universe@633 | 148 | e = cx_szmul(0, SIZE_MAX - 1, &r); |
universe@633 | 149 | EXPECT_EQ(0, e); |
universe@633 | 150 | EXPECT_EQ(0, r); |
universe@633 | 151 | |
universe@633 | 152 | e = cx_szmul(SIZE_MAX, 0, &r); |
universe@633 | 153 | EXPECT_EQ(0, e); |
universe@633 | 154 | EXPECT_EQ(0, r); |
universe@633 | 155 | |
universe@633 | 156 | e = cx_szmul(0, SIZE_MAX, &r); |
universe@633 | 157 | EXPECT_EQ(0, e); |
universe@633 | 158 | EXPECT_EQ(0, r); |
universe@633 | 159 | |
universe@633 | 160 | e = cx_szmul(0, 0, &r); |
universe@633 | 161 | EXPECT_EQ(0, e); |
universe@633 | 162 | EXPECT_EQ(0, r); |
universe@633 | 163 | } |
universe@633 | 164 | |
universe@633 | 165 | #ifdef CX_SZMUL_BUILTIN |
universe@633 | 166 | |
universe@633 | 167 | // also test the custom implementation |
universe@633 | 168 | struct Utils_szmul_impl : ::testing::Test { |
universe@633 | 169 | #undef CX_SZMUL_BUILTIN |
universe@633 | 170 | |
universe@674 | 171 | #include "../src/szmul.c" |
universe@633 | 172 | |
universe@633 | 173 | #define CX_SZMUL_BUILTIN |
universe@633 | 174 | }; |
universe@633 | 175 | |
universe@633 | 176 | TEST_F(Utils_szmul_impl, Test) { |
universe@633 | 177 | size_t r; |
universe@633 | 178 | int e; |
universe@633 | 179 | e = cx_szmul_impl(5, 7, &r); |
universe@633 | 180 | EXPECT_EQ(0, e); |
universe@633 | 181 | EXPECT_EQ(35, r); |
universe@633 | 182 | |
universe@633 | 183 | size_t s = SIZE_MAX & ~3; |
universe@633 | 184 | |
universe@633 | 185 | e = cx_szmul_impl(s / 4, 2, &r); |
universe@633 | 186 | EXPECT_EQ(0, e); |
universe@633 | 187 | EXPECT_EQ(s / 2, r); |
universe@633 | 188 | e = cx_szmul_impl(2, s / 4, &r); |
universe@633 | 189 | EXPECT_EQ(0, e); |
universe@633 | 190 | EXPECT_EQ(s / 2, r); |
universe@633 | 191 | |
universe@633 | 192 | e = cx_szmul_impl(s / 4, 4, &r); |
universe@633 | 193 | EXPECT_EQ(0, e); |
universe@633 | 194 | EXPECT_EQ(s, r); |
universe@633 | 195 | |
universe@633 | 196 | e = cx_szmul_impl(4, s / 4, &r); |
universe@633 | 197 | EXPECT_EQ(0, e); |
universe@633 | 198 | EXPECT_EQ(s, r); |
universe@633 | 199 | |
universe@633 | 200 | e = cx_szmul_impl(s / 4, 5, &r); |
universe@633 | 201 | EXPECT_NE(0, e); |
universe@633 | 202 | |
universe@633 | 203 | e = cx_szmul_impl(5, s / 4, &r); |
universe@633 | 204 | EXPECT_NE(0, e); |
universe@633 | 205 | |
universe@633 | 206 | e = cx_szmul_impl(SIZE_MAX - 4, 0, &r); |
universe@633 | 207 | EXPECT_EQ(0, e); |
universe@633 | 208 | EXPECT_EQ(0, r); |
universe@633 | 209 | |
universe@633 | 210 | e = cx_szmul_impl(0, SIZE_MAX - 1, &r); |
universe@633 | 211 | EXPECT_EQ(0, e); |
universe@633 | 212 | EXPECT_EQ(0, r); |
universe@633 | 213 | |
universe@633 | 214 | e = cx_szmul_impl(SIZE_MAX, 0, &r); |
universe@633 | 215 | EXPECT_EQ(0, e); |
universe@633 | 216 | EXPECT_EQ(0, r); |
universe@633 | 217 | |
universe@633 | 218 | e = cx_szmul_impl(0, SIZE_MAX, &r); |
universe@633 | 219 | EXPECT_EQ(0, e); |
universe@633 | 220 | EXPECT_EQ(0, r); |
universe@633 | 221 | |
universe@633 | 222 | e = cx_szmul_impl(0, 0, &r); |
universe@633 | 223 | EXPECT_EQ(0, e); |
universe@633 | 224 | EXPECT_EQ(0, r); |
universe@633 | 225 | } |
universe@633 | 226 | |
universe@633 | 227 | #endif // CX_SZMUL_BUILTIN |