| 1 |
/*************************************************************************************** |
| 2 |
* This file is dedicated to the public domain. If your jurisdiction requires a * |
| 3 |
* specific license: * |
| 4 |
* * |
| 5 |
* Copyright (c) Stephen McDowell, 2017-2024 * |
| 6 |
* License: CC0 1.0 Universal * |
| 7 |
* License Text: https://creativecommons.org/publicdomain/zero/1.0/legalcode * |
| 8 |
**************************************************************************************/ |
| 9 |
#include <sstream> |
| 10 |
|
| 11 |
#include <catch2/catch.hpp> |
| 12 |
|
| 13 |
#include <overload/overload.hpp> |
| 14 |
#include <overload/operators.hpp> |
| 15 |
|
| 16 |
/* ================================================================================== */ |
| 17 |
|
| 18 |
TEST_CASE( "void blargh()", "[cpp-func-overloads]" ) { |
| 19 |
overload::blargh(); |
| 20 |
REQUIRE( true );// compilation is the test. |
| 21 |
} |
| 22 |
|
| 23 |
/* ================================================================================== */ |
| 24 |
|
| 25 |
TEST_CASE( "int blargh(int)", "[cpp-func-overloads]" ) { |
| 26 |
int x = overload::blargh(0); |
| 27 |
REQUIRE( x == 0 ); |
| 28 |
} |
| 29 |
|
| 30 |
TEST_CASE( "int blargh(int, int)", "[cpp-func-overloads]" ) { |
| 31 |
int xy = overload::blargh(1, 2); |
| 32 |
REQUIRE( xy == 3 ); |
| 33 |
} |
| 34 |
|
| 35 |
TEST_CASE( "int blargh(int, int, int)", "[cpp-func-overloads]" ) { |
| 36 |
int xyz = overload::blargh(1, 2, 3); |
| 37 |
REQUIRE( xyz == 6 ); |
| 38 |
} |
| 39 |
|
| 40 |
TEST_CASE( "int blargh(int, int, int, int)", "[cpp-func-overloads]" ) { |
| 41 |
int xyzw = overload::blargh(1, 2, 3, 4); |
| 42 |
REQUIRE( xyzw == 10 ); |
| 43 |
} |
| 44 |
|
| 45 |
/* ================================================================================== */ |
| 46 |
|
| 47 |
TEST_CASE( "float blargh(float)", "[cpp-func-overloads]" ) { |
| 48 |
float neg_x = overload::blargh(1.0f); |
| 49 |
REQUIRE( neg_x == -1.0f ); |
| 50 |
} |
| 51 |
|
| 52 |
TEST_CASE( "float blargh(float, float)", "[cpp-func-overloads]" ) { |
| 53 |
float neg_xy = overload::blargh(1.0f, 2.0f); |
| 54 |
REQUIRE( neg_xy == -3.0f ); |
| 55 |
} |
| 56 |
|
| 57 |
TEST_CASE( "float blargh(float, float, float)", "[cpp-func-overloads]" ) { |
| 58 |
float neg_xyz = overload::blargh(1.0f, 2.0f, 3.0f); |
| 59 |
REQUIRE( neg_xyz == -6.0f ); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_CASE( "float blargh(float, float, float, float)", "[cpp-func-overloads]" ) { |
| 63 |
float neg_xyzw = overload::blargh(1.0f, 2.0f, 3.0f, 4.0f); |
| 64 |
REQUIRE( neg_xyzw == -10.0f ); |
| 65 |
} |
| 66 |
|
| 67 |
/* ================================================================================== */ |
| 68 |
|
| 69 |
TEST_CASE( "std::string blargh(const std::string &)", "[cpp-func-overloads]" ) { |
| 70 |
auto x = overload::blargh("blargh"); |
| 71 |
REQUIRE( x == "blargh" ); |
| 72 |
} |
| 73 |
|
| 74 |
TEST_CASE( "std::string blargh(const std::string &, const std::string &)", "[cpp-func-overloads]" ) { |
| 75 |
auto xy = overload::blargh("blargh ", "blargh"); |
| 76 |
REQUIRE( xy == "blargh blargh" ); |
| 77 |
} |
| 78 |
|
| 79 |
TEST_CASE( "std::string blargh(const std::string &, const std::string &, const std::string &)", "[cpp-func-overloads]" ) { |
| 80 |
auto xyz = overload::blargh("blargh ", "blargh ", "blargh"); |
| 81 |
REQUIRE( xyz == "blargh blargh blargh" ); |
| 82 |
} |
| 83 |
|
| 84 |
TEST_CASE( "std::string blargh(const std::string &, const std::string &, const std::string &, const std::string &)", "[cpp-func-overloads]" ) { |
| 85 |
auto xyzw = overload::blargh("blargh ", "blargh ", "blargh ", "blargh"); |
| 86 |
REQUIRE( xyzw == "blargh blargh blargh blargh" ); |
| 87 |
} |
| 88 |
|
| 89 |
/* ================================================================================== */ |
| 90 |
|
| 91 |
TEST_CASE( "std::size_t blargh(std::size_t, const std::string &)", "[cpp-func-overloads]" ) { |
| 92 |
auto wtf = overload::blargh(std::size_t(5), "eleven"); |
| 93 |
REQUIRE( wtf == std::size_t(11) ); |
| 94 |
} |
| 95 |
|
| 96 |
TEST_CASE( "std::size_t blargh(std::size_t, const float &, double, const std::string &)", "[cpp-func-overloads]" ) { |
| 97 |
auto wtf = overload::blargh(std::size_t(2), 12.21f, 11.11, "luncheon"); |
| 98 |
REQUIRE( wtf == std::size_t(33) ); |
| 99 |
} |
| 100 |
|
| 101 |
/* ================================================================================== */ |
| 102 |
|
| 103 |
TEST_CASE( "void blargh(std::vector<std::string> &)", "[cpp-func-overloads]" ) { |
| 104 |
std::vector<std::string> v; |
| 105 |
REQUIRE( v.size() == 0); |
| 106 |
|
| 107 |
overload::blargh(v); |
| 108 |
REQUIRE( v.size() == 1 ); |
| 109 |
REQUIRE( v[0] == "blargh" ); |
| 110 |
|
| 111 |
v.emplace_back("narp"); |
| 112 |
REQUIRE( v.size() == 2 ); |
| 113 |
REQUIRE( v[0] == "blargh" ); |
| 114 |
REQUIRE( v[1] == "narp" ); |
| 115 |
|
| 116 |
overload::blargh(v); |
| 117 |
REQUIRE( v.size() == 3 ); |
| 118 |
REQUIRE( v[0] == "blargh" ); |
| 119 |
REQUIRE( v[1] == "narp" ); |
| 120 |
REQUIRE( v[2] == "blargh" ); |
| 121 |
|
| 122 |
v.emplace_back("yarp"); |
| 123 |
REQUIRE( v.size() == 4 ); |
| 124 |
REQUIRE( v[0] == "blargh" ); |
| 125 |
REQUIRE( v[1] == "narp" ); |
| 126 |
REQUIRE( v[2] == "blargh" ); |
| 127 |
REQUIRE( v[3] == "yarp" ); |
| 128 |
|
| 129 |
overload::blargh(v); |
| 130 |
REQUIRE( v.size() == 5 ); |
| 131 |
REQUIRE( v[0] == "blargh" ); |
| 132 |
REQUIRE( v[1] == "narp" ); |
| 133 |
REQUIRE( v[2] == "blargh" ); |
| 134 |
REQUIRE( v[3] == "yarp" ); |
| 135 |
REQUIRE( v[4] == "blargh" ); |
| 136 |
} |
| 137 |
|
| 138 |
TEST_CASE( "void blargh(std::vector<std::vector<int>>&)", "[cpp-func-overloads]" ) { |
| 139 |
std::vector<std::vector<int>> v; |
| 140 |
REQUIRE( v.size() == 0 ); |
| 141 |
|
| 142 |
overload::blargh(v); |
| 143 |
REQUIRE( v.size() == 1 ); |
| 144 |
REQUIRE( v[0].size() == 1 ); |
| 145 |
REQUIRE( v[0][0] == 0 ); |
| 146 |
|
| 147 |
v[0].emplace_back(1); |
| 148 |
REQUIRE( v.size() == 1 ); |
| 149 |
REQUIRE( v[0].size() == 2 ); |
| 150 |
REQUIRE( v[0][0] == 0 ); |
| 151 |
REQUIRE( v[0][1] == 1 ); |
| 152 |
|
| 153 |
overload::blargh(v); |
| 154 |
REQUIRE( v.size() == 2 ); |
| 155 |
REQUIRE( v[0].size() == 2 ); |
| 156 |
REQUIRE( v[0][0] == 0 ); |
| 157 |
REQUIRE( v[0][1] == 1 ); |
| 158 |
REQUIRE( v[1].size() == 1 ); |
| 159 |
REQUIRE( v[1][0] == 0 ); |
| 160 |
|
| 161 |
v[1].emplace_back(2); |
| 162 |
REQUIRE( v.size() == 2 ); |
| 163 |
REQUIRE( v[0].size() == 2 ); |
| 164 |
REQUIRE( v[0][0] == 0 ); |
| 165 |
REQUIRE( v[0][1] == 1 ); |
| 166 |
REQUIRE( v[1].size() == 2 ); |
| 167 |
REQUIRE( v[1][0] == 0 ); |
| 168 |
REQUIRE( v[1][1] == 2 ); |
| 169 |
} |
| 170 |
|
| 171 |
/* ================================================================================== */ |
| 172 |
|
| 173 |
TEST_CASE( "void blargh(const float*, const float*, float*, std::size_t)", "[cpp-func-overloads]" ) { |
| 174 |
float x = 1.0f; |
| 175 |
float y = 2.0f; |
| 176 |
float z = 0.0f; |
| 177 |
overload::blargh(&x, &y, &z, 1); |
| 178 |
REQUIRE(z == 3.0f); |
| 179 |
|
| 180 |
static constexpr std::size_t N = 33; |
| 181 |
float *xs = (float *)malloc(N * sizeof(float)); |
| 182 |
float *ys = (float *)malloc(N * sizeof(float)); |
| 183 |
float *zs = (float *)malloc(N * sizeof(float)); |
| 184 |
for (std::size_t i = 0; i < N; ++i) { |
| 185 |
xs[i] = static_cast<float>(i); |
| 186 |
ys[i] = static_cast<float>(N - i); |
| 187 |
zs[i] = 0.0f; |
| 188 |
} |
| 189 |
|
| 190 |
overload::blargh(xs, ys, zs, N); |
| 191 |
for (std::size_t i = 0; i < N; ++i) { |
| 192 |
CHECK( zs[i] == static_cast<float>(i + (N - i)) ); |
| 193 |
} |
| 194 |
|
| 195 |
free(xs); |
| 196 |
free(ys); |
| 197 |
free(zs); |
| 198 |
} |
| 199 |
|
| 200 |
/* ================================================================================== */ |
| 201 |
|
| 202 |
TEST_CASE( "template <class C> typename C::type blargh(typename C::type)", "[cpp-func-overloads]" ) { |
| 203 |
struct C_float { using type = float; }; |
| 204 |
auto v_f = overload::blargh<C_float>(11.11f); |
| 205 |
REQUIRE( v_f == 11.11f ); |
| 206 |
|
| 207 |
struct C_int { using type = int; }; |
| 208 |
auto v_i = overload::blargh<C_int>(12); |
| 209 |
REQUIRE( v_i == 12 ); |
| 210 |
|
| 211 |
struct C_string { using type = std::string; }; |
| 212 |
auto v_s = overload::blargh<C_string>("hiya"); |
| 213 |
REQUIRE( v_s == "hiya" ); |
| 214 |
|
| 215 |
struct Blargh { std::string name = "jajaja"; }; |
| 216 |
struct C_Blargh { using type = Blargh; }; |
| 217 |
|
| 218 |
auto b = Blargh(); |
| 219 |
auto v_Blargh = overload::blargh<C_Blargh>(b); |
| 220 |
REQUIRE( v_Blargh.name == "jajaja" ); |
| 221 |
REQUIRE( &v_Blargh != &b ); |
| 222 |
|
| 223 |
b.name = "Snow, John"; |
| 224 |
v_Blargh = overload::blargh<C_Blargh>(b); |
| 225 |
REQUIRE( v_Blargh.name == "Snow, John" ); |
| 226 |
REQUIRE( &v_Blargh != &b ); |
| 227 |
} |
| 228 |
|
| 229 |
TEST_CASE( "template <> blargh<SuperStruct>(typename C::type)", "[cpp-func-overloads]" ) { |
| 230 |
auto x = overload::blargh<overload::SuperStruct>(111); |
| 231 |
REQUIRE( x == 111 ); |
| 232 |
} |
| 233 |
|
| 234 |
TEST_CASE( "template <> blargh<nested::SuperStruct>(typename C::type)", "[cpp-func-overloads]" ) { |
| 235 |
auto x = overload::blargh<overload::nested::SuperStruct>(111); |
| 236 |
REQUIRE( x == 111 ); |
| 237 |
} |
| 238 |
|
| 239 |
TEST_CASE( "template <class C, typename T> T blargh(typename C::type)", "[cpp-func-overloads]" ) { |
| 240 |
struct C_float { using type = float; }; |
| 241 |
|
| 242 |
double v_f2d = overload::blargh<C_float, double>(11.0f); |
| 243 |
REQUIRE( v_f2d == 11.0 ); |
| 244 |
|
| 245 |
std::string v_f2s = overload::blargh<C_float, std::string>(11.0f); |
| 246 |
REQUIRE( v_f2s == "" ); |
| 247 |
|
| 248 |
struct C_int { using type = int; }; |
| 249 |
|
| 250 |
long v_i2l = overload::blargh<C_int, long>(12); |
| 251 |
REQUIRE( v_i2l == 12 ); |
| 252 |
|
| 253 |
std::string v_i2s = overload::blargh<C_int, std::string>(12); |
| 254 |
REQUIRE( v_i2s == "" ); |
| 255 |
|
| 256 |
struct C_string { using type = std::string; }; |
| 257 |
|
| 258 |
std::basic_string<char> v_s2c = overload::blargh<C_string, std::basic_string<char>>("hiya"); |
| 259 |
REQUIRE( v_s2c == "hiya" ); |
| 260 |
|
| 261 |
int v_s2i = overload::blargh<C_string, int>("hiya"); |
| 262 |
REQUIRE( v_s2i == 0 ); |
| 263 |
} |
| 264 |
|
| 265 |
/* ================================================================================== */ |
| 266 |
|
| 267 |
TEST_CASE( "int ::blargh(int)", "[cpp-func-overloads]" ) { |
| 268 |
int x = blargh(0); |
| 269 |
REQUIRE( x == 0 ); |
| 270 |
|
| 271 |
x = blargh(12); |
| 272 |
REQUIRE( x == 12 ); |
| 273 |
} |
| 274 |
|
| 275 |
/* ================================================================================== */ |
| 276 |
|
| 277 |
TEST_CASE( "Operator overloads", "[cpp-func-overloads]") { |
| 278 |
using overload::CustomType; |
| 279 |
CustomType eleven{11}; |
| 280 |
CustomType eleven_alt{11}; |
| 281 |
CustomType twenty_two{22}; |
| 282 |
CustomType twenty_two_alt{22}; |
| 283 |
|
| 284 |
std::ostringstream oss; |
| 285 |
oss << "Eleven: " << eleven << ", Twenty Two: " << twenty_two; |
| 286 |
const std::string value = oss.str(); |
| 287 |
REQUIRE( value == "Eleven: 11, Twenty Two: 22"); |
| 288 |
|
| 289 |
REQUIRE( eleven == eleven_alt ); |
| 290 |
REQUIRE( twenty_two == twenty_two_alt ); |
| 291 |
|
| 292 |
REQUIRE( eleven != twenty_two); |
| 293 |
|
| 294 |
REQUIRE( eleven < twenty_two ); |
| 295 |
|
| 296 |
REQUIRE( twenty_two > eleven ); |
| 297 |
|
| 298 |
REQUIRE( eleven <= eleven_alt ); |
| 299 |
REQUIRE( eleven <= twenty_two ); |
| 300 |
|
| 301 |
REQUIRE( eleven >= eleven_alt ); |
| 302 |
REQUIRE( twenty_two >= eleven ); |
| 303 |
} |