1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/***********************************************************************
* fdata-test.cpp - FData unit tests *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2020-2022 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* FINAL CUT 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
#include <utility>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestFixture.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <final/final.h>
//----------------------------------------------------------------------
// functions
//----------------------------------------------------------------------
auto my_function() -> float
{
return 13.45F;
}
//----------------------------------------------------------------------
auto my_function2 (long int i) -> long int
{
return 2 * i;
}
//----------------------------------------------------------------------
auto my_function3 (long int i) -> long int
{
return 3 * i;
}
//----------------------------------------------------------------------
// class FDataTest
//----------------------------------------------------------------------
class FDataTest : public CPPUNIT_NS::TestFixture
{
public:
FDataTest() = default;
protected:
void classNameTest();
void fdataTest();
void copyConstructorTest();
void moveConstructorTest();
void copyAssignmentTest();
void moveAssignmentTest();
void makeFDataTest();
private:
// Adds code needed to register the test suite
CPPUNIT_TEST_SUITE (FDataTest);
// Add a methods to the test suite
CPPUNIT_TEST (classNameTest);
CPPUNIT_TEST (fdataTest);
CPPUNIT_TEST (copyConstructorTest);
CPPUNIT_TEST (moveConstructorTest);
CPPUNIT_TEST (copyAssignmentTest);
CPPUNIT_TEST (moveAssignmentTest);
CPPUNIT_TEST (makeFDataTest);
// End of test suite definition
CPPUNIT_TEST_SUITE_END();
// Data member
static finalcut::FWidget root_widget;
};
// static class attributes
finalcut::FWidget FDataTest::root_widget{nullptr};
//----------------------------------------------------------------------
void FDataTest::classNameTest()
{
auto da = finalcut::FDataAccess();
const finalcut::FString& classname2 = da.getClassName();
CPPUNIT_ASSERT ( classname2 == "FDataAccess" );
auto d = finalcut::FData<void*>(nullptr);
const finalcut::FString& classname = d.getClassName();
CPPUNIT_ASSERT ( classname == "FData" );
}
//----------------------------------------------------------------------
void FDataTest::fdataTest()
{
// nummber
auto data1 = finalcut::FData<int>(5);
CPPUNIT_ASSERT ( data1.isInitializedCopy() );
CPPUNIT_ASSERT ( ! data1.isInitializedReference() );
std::stringstream stream{};
stream << data1;
CPPUNIT_ASSERT ( stream.str() == "5" );
int i1{data1};
CPPUNIT_ASSERT ( i1 == 5 );
CPPUNIT_ASSERT ( data1() == 5 );
CPPUNIT_ASSERT ( data1.get() == 5 );
data1.get()++;
CPPUNIT_ASSERT ( data1.get() == 6 );
data1.set(3);
CPPUNIT_ASSERT ( data1.get() == 3 );
data1 << 8;
CPPUNIT_ASSERT ( data1.get() == 8 );
// int value
int integer_value = 10;
auto data2 = finalcut::FData<int>(std::ref(integer_value));
CPPUNIT_ASSERT ( ! data2.isInitializedCopy() );
CPPUNIT_ASSERT ( data2.isInitializedReference() );
stream.clear();
stream.str("");
stream << data2;
CPPUNIT_ASSERT ( stream.str() == "10" );
int i2{data2};
CPPUNIT_ASSERT ( i2 == 10 );
CPPUNIT_ASSERT ( data2() == 10 );
CPPUNIT_ASSERT ( data2.get() == 10 );
data2.get()--;
CPPUNIT_ASSERT ( data2.get() == 9 );
CPPUNIT_ASSERT ( integer_value == 9 );
data2.set(7);
CPPUNIT_ASSERT ( data2.get() == 7 );
CPPUNIT_ASSERT ( integer_value == 7 );
data2 << 15;
CPPUNIT_ASSERT ( integer_value == 15 );
// const int value
const int const_integer_value = 12;
auto data3 = finalcut::FData<const int>(const_integer_value);
CPPUNIT_ASSERT ( ! data3.isInitializedCopy() );
CPPUNIT_ASSERT ( data3.isInitializedReference() );
stream.clear();
stream.str("");
stream << data3;
CPPUNIT_ASSERT ( stream.str() == "12" );
int i3{data3};
CPPUNIT_ASSERT ( i3 == 12 );
CPPUNIT_ASSERT ( data3() == 12 );
CPPUNIT_ASSERT ( data3.get() == 12 );
// Function via pointer
auto data4 = finalcut::FData<std::function<float()>>(&my_function);
CPPUNIT_ASSERT ( data4.isInitializedCopy() );
CPPUNIT_ASSERT ( ! data4.isInitializedReference() );
stream.clear();
stream.str("");
stream << data4()();
CPPUNIT_ASSERT ( stream.str() == "13.45" );
CPPUNIT_ASSERT ( data4()() == 13.45F );
CPPUNIT_ASSERT ( data4.get()() == 13.45F );
// Function via reference -> remove reference + add pointer
auto data5 = finalcut::FData<std::function<float()>>(my_function);
CPPUNIT_ASSERT ( data5.isInitializedCopy() );
CPPUNIT_ASSERT ( ! data5.isInitializedReference() );
stream.clear();
stream.str("");
stream << data5()();
CPPUNIT_ASSERT ( stream.str() == "13.45" );
CPPUNIT_ASSERT ( data5()() == 13.45F );
CPPUNIT_ASSERT ( data5.get()() == 13.45F );
// Function with parameter via pointer
auto data6 = finalcut::FData<std::function<long int(long int)>>(&my_function2);
CPPUNIT_ASSERT ( data6.isInitializedCopy() );
CPPUNIT_ASSERT ( ! data6.isInitializedReference() );
stream.clear();
stream.str("");
stream << data6()(4L);
CPPUNIT_ASSERT ( stream.str() == "8" );
CPPUNIT_ASSERT ( data6()(4) == 8L );
CPPUNIT_ASSERT ( data6.get()(4) == 8L );
// std::string
auto data7 = finalcut::FData<std::string>("123");
CPPUNIT_ASSERT ( data7.isInitializedCopy() );
CPPUNIT_ASSERT ( ! data7.isInitializedReference() );
stream.clear();
stream.str("");
stream << data7;
CPPUNIT_ASSERT ( stream.str() == "123" );
std::string str{data7};
CPPUNIT_ASSERT ( str == "123" );
CPPUNIT_ASSERT ( data7() == "123" );
CPPUNIT_ASSERT ( data7.get() == "123" );
data7.get() += "4";
CPPUNIT_ASSERT ( data7.get() == "1234" );
data7.set("abc");
CPPUNIT_ASSERT ( data7.get() == "abc" );
data7 << "xyz";
CPPUNIT_ASSERT ( data7.get() == "xyz" );
}
//----------------------------------------------------------------------
void FDataTest::copyConstructorTest()
{
// value copy
auto data1 = finalcut::FData<int>(2);
CPPUNIT_ASSERT ( data1.isInitializedCopy() );
CPPUNIT_ASSERT ( data1.get() == 2 );
auto data2 = finalcut::FData<int>(data1);
CPPUNIT_ASSERT ( data2.isInitializedCopy() );
data1.get()++;
CPPUNIT_ASSERT ( data1.get() == 3 );
CPPUNIT_ASSERT ( data2.get() == 2 );
// reference copy
uInt n = 100;
auto data3 = finalcut::FData<uInt>(n);
CPPUNIT_ASSERT ( data3.isInitializedReference() );
CPPUNIT_ASSERT ( data3.get() == 100 );
auto data4 = finalcut::FData<uInt>(data3);
CPPUNIT_ASSERT ( data4.isInitializedReference() );
data3.get()--;
CPPUNIT_ASSERT ( data3.get() == 99 );
CPPUNIT_ASSERT ( data4.get() == 99 );
}
//----------------------------------------------------------------------
void FDataTest::moveConstructorTest()
{
// value copy
auto data1 = finalcut::FData<int>(-5);
CPPUNIT_ASSERT ( data1.isInitializedCopy() );
CPPUNIT_ASSERT ( data1.get() == -5 );
auto data1_copy = data1;
auto data2 = finalcut::FData<int>(std::move(data1));
CPPUNIT_ASSERT ( data2.isInitializedCopy() );
data1_copy.get() += 10;
CPPUNIT_ASSERT ( data1_copy.get() == 5 );
CPPUNIT_ASSERT ( data2.get() == -5 );
// reference copy
long int n = 0xfffffff;
auto data3 = finalcut::FData<long int>(n);
CPPUNIT_ASSERT ( data3.isInitializedReference() );
CPPUNIT_ASSERT ( data3.get() == 0xfffffff );
auto data3_copy = data3;
auto data4 = finalcut::FData<long int>(std::move(data3));
CPPUNIT_ASSERT ( data4.isInitializedReference() );
data3_copy.get()++;
CPPUNIT_ASSERT ( data3_copy.get() == 0x10000000 );
CPPUNIT_ASSERT ( data4.get() == 0x10000000 );
}
//----------------------------------------------------------------------
void FDataTest::copyAssignmentTest()
{
// value copy
auto data1 = finalcut::FData<char>(123);
CPPUNIT_ASSERT ( data1.isInitializedCopy() );
CPPUNIT_ASSERT ( data1.get() == 123 );
finalcut::FData<char> data2{0};
data2 = data1;
CPPUNIT_ASSERT ( data2.isInitializedCopy() );
data1.get() -= 100;
CPPUNIT_ASSERT ( data1.get() == 23 );
CPPUNIT_ASSERT ( data2.get() == 123 );
// reference copy
double c = 299792458.0; // Speed of light
auto data3 = finalcut::FData<double>(c);
CPPUNIT_ASSERT ( data3.isInitializedReference() );
CPPUNIT_ASSERT ( data3.get() == 299792458 );
finalcut::FData<double> data4{0.0};
data4 = data3;
CPPUNIT_ASSERT ( data4.isInitializedReference() );
data4.get() -= 343.2; // Speed of sound
CPPUNIT_ASSERT ( data3.get() == 299792114.8 );
CPPUNIT_ASSERT ( data4.get() == 299792114.8 );
}
//----------------------------------------------------------------------
void FDataTest::moveAssignmentTest()
{
// value copy
auto data1 = finalcut::FData<float>(9.81F);
CPPUNIT_ASSERT ( data1.isInitializedCopy() );
CPPUNIT_ASSERT ( data1.get() == 9.81F );
finalcut::FData<float> data2{0};
auto data1_copy = data1;
data2 = std::move(data1);
CPPUNIT_ASSERT ( data2.isInitializedCopy() );
data1_copy.get() -= 0.81;
CPPUNIT_ASSERT ( data1_copy.get() == 9.0F );
CPPUNIT_ASSERT ( data2.get() == 9.81F );
// reference copy
auto au = 149597870700LL; // Astronomical unit
auto data3 = finalcut::FData<long long int>(au);
CPPUNIT_ASSERT ( data3.isInitializedReference() );
CPPUNIT_ASSERT ( data3.get() == 149597870700LL );
finalcut::FData<long long int> data4{0};
auto data3_copy = data3;
data4 = std::move(data3);
CPPUNIT_ASSERT ( data4.isInitializedReference() );
data4.get() /= 2LL;
CPPUNIT_ASSERT ( data3_copy.get() == 74798935350LL );
CPPUNIT_ASSERT ( data4.get() == 74798935350LL );
}
//----------------------------------------------------------------------
void FDataTest::makeFDataTest()
{
finalcut::FDataAccess* data_pointer{nullptr};
// Array
using ThreeInts = int[3];
ThreeInts int_array{2, 1, 4};
data_pointer = finalcut::makeFData(int_array);
const auto& ints = static_cast<finalcut::FData<finalcut::clean_fdata_t<ThreeInts>>&>(*data_pointer).get();
CPPUNIT_ASSERT ( ints[0] == 2 );
CPPUNIT_ASSERT ( ints[1] == 1 );
CPPUNIT_ASSERT ( ints[2] == 4 );
delete data_pointer;
// Integer
int n = 9;
data_pointer = finalcut::makeFData(std::move(n));
auto& data = static_cast<finalcut::FData<int>&>(*data_pointer);
CPPUNIT_ASSERT ( data.isInitializedCopy() );
CPPUNIT_ASSERT ( ! data.isInitializedReference() );
auto& n2 = data.get();
CPPUNIT_ASSERT ( n2 == 9 );
n2++;
CPPUNIT_ASSERT ( n2 == 10 );
CPPUNIT_ASSERT ( data() == 10 );
CPPUNIT_ASSERT ( data_pointer->get<int>() == 10 );
data_pointer->set<int>(33);
CPPUNIT_ASSERT ( data_pointer->get<int>() == 33 );
delete data_pointer;
// Function
data_pointer = finalcut::makeFData(my_function2);
const auto& func = static_cast<finalcut::FData<finalcut::clean_fdata_t<long int (*) (long int)>>&>(*data_pointer).get();
CPPUNIT_ASSERT ( func(128) == 256 );
data_pointer->set<long int (*) (long int)>(&my_function3);
CPPUNIT_ASSERT ( func(128) == 384 );
delete data_pointer;
}
// Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION (FDataTest);
// The general unit test main part
#include <main-test.inc>
|