Debian Codegraph @

testing/projects/cpp_nesting/src/tests.cpp

raw
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 <catch2/catch.hpp>
10 #include <type_traits>
11 #include <vector>
12
13 /* ================================================================================== */
14
15 #include <top_level.hpp>
16 TEST_CASE( "top_level", "[cpp-nesting]" ) {
17 top_level tl("hiya");
18 REQUIRE( tl.mName == "hiya" );
19 }
20
21 /* ================================================================================== */
22
23 #include <nesting_specializations.hpp>
24 TEST_CASE( "nesting_specializations", "[cpp-nesting]" ) {
25 REQUIRE(special::Normal().id() == "Normal");
26 REQUIRE(special::Normal::Nested().id() == "Nested");
27 REQUIRE(special::Normal::Nested::Like().id() == "Like");
28 REQUIRE(special::Normal::Nested::Like::Usual().id() == "Usual");
29
30 using VGA = special::Image<640, 480>;
31 REQUIRE(VGA::width == 640);
32 REQUIRE(VGA::height == 480);
33
34 // 2 channels is just awkward.
35 using WeirdBuffer = special::ImageBuffer<VGA, float, 2>;
36 REQUIRE(std::is_same_v<WeirdBuffer::Image_t, VGA>);
37 REQUIRE(WeirdBuffer::width == 640);
38 REQUIRE(WeirdBuffer::height == 480);
39 REQUIRE(std::is_same_v<WeirdBuffer::data_t, float>);
40 REQUIRE(WeirdBuffer::channels == 2);
41 WeirdBuffer::Data weird_data;// compilation test for changing Data
42 weird_data.vals[0] = 111.111f;
43 weird_data.vals[1] = 222.222f;
44 REQUIRE(std::abs(weird_data.vals[0] - 111.111f) < 1e-6);
45 REQUIRE(std::abs(weird_data.vals[1] - 222.222f) < 1e-6);
46 REQUIRE(WeirdBuffer::SomeThing<3>().id() == "3");
47 REQUIRE(WeirdBuffer().size() == 640 * 480 * 2 * sizeof(float));
48
49 // what a name!
50 using PngVgaRgba = special::ImageBuffer<VGA, uint8_t, 4>;
51 REQUIRE(std::is_same_v<PngVgaRgba::Image_t, VGA>);
52 REQUIRE(PngVgaRgba::width == 640);
53 REQUIRE(PngVgaRgba::height == 480);
54 REQUIRE(std::is_same_v<PngVgaRgba::data_t, uint8_t>);
55 REQUIRE(PngVgaRgba::channels == 4);
56 PngVgaRgba::Data pix{1, 2, 3, 4};// compilation test for changing Data
57 REQUIRE(pix.r == 1);
58 REQUIRE(pix.g == 2);
59 REQUIRE(pix.b == 3);
60 REQUIRE(pix.a == 4);
61 REQUIRE(PngVgaRgba::SomeThing().id() == "I AM SPECIAL!");
62 REQUIRE(PngVgaRgba().size() == 640 * 480 * 4);
63
64 // fully special
65 using Wow = special::ImageBuffer<special::Image<1920, 1080>, float, 128>;
66 REQUIRE(std::is_same_v<Wow::Image_t, special::Image<1920, 1080>>);
67 REQUIRE(Wow::width == 1920);
68 REQUIRE(Wow::height == 1080);
69 REQUIRE(std::is_same_v<Wow::data_t, float>);
70 Wow::Data wow_data;
71 wow_data.samples.emplace_back(0.0f);
72 REQUIRE(std::abs(wow_data.samples[0]) < 1e-6);
73 REQUIRE(Wow::SomeThing().id() == "Spectral");
74 REQUIRE(Wow().size() == 88);
75
76 // Base without specialization.
77 REQUIRE(special::Base<1>().size() == 1);
78
79 // Base with specialization.
80 using B2 = special::Base<2>;
81 REQUIRE(B2().size() == 42);
82 REQUIRE(B2::InnerStruct().a() == 3);
83 REQUIRE(B2::AnotherNestedStruct().b() == 4);
84 REQUIRE(B2::InnerTemplatedStruct<3>().s() == '{');
85 REQUIRE(B2::InnerTemplatedStruct<4>().s() == '}');
86
87 // special::unique::snowflake::Ontology
88 using SSO1 = special::unique::snowflake::Ontology<1>;
89 SSO1 onsies;
90 REQUIRE(SSO1::epoch() == onsies.epoch());
91 REQUIRE(SSO1::epoch() == 1);
92 REQUIRE(onsies.dual() == 1);
93
94 using SSO11 = special::unique::snowflake::Ontology<11>;
95 SSO11 elevensies;
96 REQUIRE(SSO11::epoch() == elevensies.epoch());
97 REQUIRE(SSO11::epoch() == 11);
98 REQUIRE(elevensies.dual() == 22);
99
100 // special::unique::Nonsense
101 using NonsenseVec = special::unique::Nonsense<11, std::vector<int>>;
102 std::vector<int> mein_vector = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
103 NonsenseVec n_vec{mein_vector};
104 REQUIRE(n_vec.x() == 11);
105 REQUIRE(n_vec.t().size() == 11);
106
107 using SSO22 = special::unique::snowflake::Ontology<22>;
108 using PartialNonsense = special::unique::PartialNonsense<22>;
109 SSO22 ssdd;
110 PartialNonsense pn{ssdd};
111 REQUIRE(pn.x() == ssdd.epoch() + pn.t().dual() + 22);
112
113 using FullNonsense = special::unique::FullNonsense;
114 FullNonsense fn{elevensies};
115 REQUIRE(fn.x() == elevensies.epoch() + fn.t().dual());
116
117 // special::complex
118 REQUIRE(special::complex::Fold<int, double, float>::sum(1, 2.0, 3.0f) == 6.0);
119
120 struct NoTypeMember{ };
121 struct TypeMember { using type = int; };
122 REQUIRE(special::complex::has_type_member<NoTypeMember>().meh() == false);
123 REQUIRE(special::complex::has_type_member<TypeMember>().meh() == true);
124
125 static int x = 11;
126 REQUIRE(special::complex::IntPtr<&x>().i() == 11);
127 REQUIRE(special::complex::IntRef<x>().i() == 11);
128 }
129
130 /* ================================================================================== */
131
132 #include <nested/one/one.hpp>
133 TEST_CASE( "nested/one/one.hpp", "[cpp-nesting]" ) {
134 nested::one one;
135 REQUIRE( one.compiles() );
136
137 nested::one::params one_params;
138 REQUIRE( one_params.compiles() );
139
140 REQUIRE( sizeof(nested::one::params::four_bytes) == 4 );
141 }
142
143 /* ================================================================================== */
144
145 #include <nested/two/two.hpp>
146 TEST_CASE( "nested/two/two.hpp", "[cpp-nesting]" ) {
147 nested::two two;
148 REQUIRE( two.compiles() );
149
150 nested::two::params two_params;
151 REQUIRE( two_params.compiles() );
152
153 REQUIRE( sizeof(nested::four_bytes) == 4 );
154 REQUIRE( sizeof(nested::two::params::four_bytes) == 4 );
155 }
156
157 /* ================================================================================== */
158
159 #include <nested/dual_nested/one/one.hpp>
160 TEST_CASE( "nested/dual_nested/one/one.hpp", "[cpp-nesting]" ) {
161 nested::dual_nested::one one;
162 REQUIRE( one.compiles() );
163
164 nested::dual_nested::one::params one_params;
165 REQUIRE( one_params.compiles() );
166
167 REQUIRE( sizeof(nested::dual_nested::one::params::four_bytes) == 4 );
168 }
169
170 /* ================================================================================== */
171
172 #include <nested/dual_nested/two/two.hpp>
173 TEST_CASE( "nested/dual_nested/two/two.hpp", "[cpp-nesting]" ) {
174 nested::dual_nested::two two;
175 REQUIRE( two.compiles() );
176
177 nested::dual_nested::two::params two_params;
178 REQUIRE( two_params.compiles() );
179
180 REQUIRE( sizeof(nested::dual_nested::two::params::four_bytes) == 4 );
181 }