Commit 30c1f856d469d181b2c8520e7c1361e436f559bf
1 parent
653ce355
See if C++11 features work
Showing
3 changed files
with
320 additions
and
0 deletions
libtests/build.mk
libtests/cxx11.cc
0 → 100644
| 1 | +#include <iostream> | |
| 2 | +#include <cassert> | |
| 3 | +#include <functional> | |
| 4 | +#include <type_traits> | |
| 5 | +#include <cstdint> | |
| 6 | +#include <vector> | |
| 7 | +#include <map> | |
| 8 | +#include <memory> | |
| 9 | + | |
| 10 | +// Functional programming | |
| 11 | + | |
| 12 | +// Function that returns a callable in the form of a lambda | |
| 13 | +std::function<int (int)> make_adder(int x) | |
| 14 | +{ | |
| 15 | + return ([=](int a) -> int{ return x + a; }); | |
| 16 | +} | |
| 17 | + | |
| 18 | +void do_functional() | |
| 19 | +{ | |
| 20 | + // Lambda with no capture | |
| 21 | + auto simple_lambda = [](int a){ return a + 3; }; | |
| 22 | + assert(simple_lambda(5) == 8); | |
| 23 | + | |
| 24 | + // Capture by value | |
| 25 | + int x = 5; | |
| 26 | + auto by_value = [x](int a){ return a + x; }; | |
| 27 | + assert(by_value(1) == 6); | |
| 28 | + x = 7; // change not seen by lambda | |
| 29 | + assert(by_value(1) == 6); | |
| 30 | + // Also >> at end of template | |
| 31 | + assert((std::is_convertible<decltype(by_value), | |
| 32 | + std::function<int(int)>>::value)); | |
| 33 | + | |
| 34 | + // Capture by reference | |
| 35 | + auto by_reference = [&x](int a){ return a + x; }; | |
| 36 | + assert(by_reference(1) == 8); | |
| 37 | + x = 8; // change seen my lambda | |
| 38 | + assert(by_reference(1) == 9); | |
| 39 | + | |
| 40 | + // Get a callable from a function | |
| 41 | + auto add3 = make_adder(3); | |
| 42 | + assert(add3(5) == 8); | |
| 43 | + | |
| 44 | + auto make_addr_lambda = [](int a) { | |
| 45 | + return [a](int b) { return a + b; }; | |
| 46 | + }; | |
| 47 | + | |
| 48 | + assert(make_addr_lambda(6)(8) == 14); | |
| 49 | +} | |
| 50 | + | |
| 51 | +// Integer types, type traits | |
| 52 | + | |
| 53 | +template <typename T> | |
| 54 | +void check_size(size_t size, bool is_signed) | |
| 55 | +{ | |
| 56 | + assert(sizeof(T) == size); | |
| 57 | + assert(std::is_signed<T>::value == is_signed); | |
| 58 | +} | |
| 59 | + | |
| 60 | +void do_inttypes() | |
| 61 | +{ | |
| 62 | + // static_assert is a compile-time check | |
| 63 | + static_assert(1 == sizeof(int8_t), "int8_t check"); | |
| 64 | + check_size<int8_t>(1, true); | |
| 65 | + check_size<uint8_t>(1, false); | |
| 66 | + check_size<int16_t>(2, true); | |
| 67 | + check_size<uint16_t>(2, false); | |
| 68 | + check_size<int32_t>(4, true); | |
| 69 | + check_size<uint32_t>(4, false); | |
| 70 | + check_size<int64_t>(8, true); | |
| 71 | + check_size<uint64_t>(8, false); | |
| 72 | + | |
| 73 | + // auto, decltype | |
| 74 | + auto x = 5LL; | |
| 75 | + check_size<decltype(x)>(8, true); | |
| 76 | + assert((std::is_same<long long, decltype(x)>::value)); | |
| 77 | +} | |
| 78 | + | |
| 79 | +class A | |
| 80 | +{ | |
| 81 | + public: | |
| 82 | + static constexpr auto def_value = 5; | |
| 83 | + A(int x) : | |
| 84 | + x(x) | |
| 85 | + { | |
| 86 | + } | |
| 87 | + // Constructor delegation | |
| 88 | + A() : A(def_value) | |
| 89 | + { | |
| 90 | + } | |
| 91 | + int getX() const | |
| 92 | + { | |
| 93 | + return x; | |
| 94 | + } | |
| 95 | + | |
| 96 | + private: | |
| 97 | + int x; | |
| 98 | +}; | |
| 99 | + | |
| 100 | +void do_iteration() | |
| 101 | +{ | |
| 102 | + // Initializers, foreach syntax, auto for iterators | |
| 103 | + std::vector<int> v = { 1, 2, 3, 4 }; | |
| 104 | + assert(v.size() == 4); | |
| 105 | + int sum = 0; | |
| 106 | + for (auto i: v) | |
| 107 | + { | |
| 108 | + sum += i; | |
| 109 | + } | |
| 110 | + assert(10 == sum); | |
| 111 | + for (auto i = v.begin(); i != v.end(); ++i) | |
| 112 | + { | |
| 113 | + sum += *i; | |
| 114 | + } | |
| 115 | + assert(20 == sum); | |
| 116 | + | |
| 117 | + std::vector<A> v2 = { A(), A(3) }; | |
| 118 | + assert(5 == v2.at(0).getX()); | |
| 119 | + assert(3 == v2.at(1).getX()); | |
| 120 | +} | |
| 121 | + | |
| 122 | +// Variadic template | |
| 123 | + | |
| 124 | +template<class A1> | |
| 125 | +void variadic1(A1 const& a1) | |
| 126 | +{ | |
| 127 | + assert(a1 == 12); | |
| 128 | +} | |
| 129 | + | |
| 130 | +template<class A1, class A2> | |
| 131 | +void variadic1(A1 const& a1, A2 const& a2) | |
| 132 | +{ | |
| 133 | + assert(a1 == a2); | |
| 134 | +} | |
| 135 | + | |
| 136 | +template<class ...Args> | |
| 137 | +void variadic(Args... args) | |
| 138 | +{ | |
| 139 | + variadic1(args...); | |
| 140 | +} | |
| 141 | + | |
| 142 | +template<class A> | |
| 143 | +bool pairwise_equal(A const& a, A const& b) | |
| 144 | +{ | |
| 145 | + return (a == b); | |
| 146 | +} | |
| 147 | + | |
| 148 | +template<class T, class ...Rest> | |
| 149 | +bool pairwise_equal(T const& a, T const& b, Rest... rest) | |
| 150 | +{ | |
| 151 | + return pairwise_equal(a, b) && pairwise_equal(rest...); | |
| 152 | +} | |
| 153 | + | |
| 154 | +void do_variadic() | |
| 155 | +{ | |
| 156 | + variadic(15, 15); | |
| 157 | + variadic(12); | |
| 158 | + assert(pairwise_equal(5, 5, 2.0, 2.0, std::string("a"), std::string("a"))); | |
| 159 | + assert(! pairwise_equal(5, 5, 2.0, 3.0)); | |
| 160 | +} | |
| 161 | + | |
| 162 | +// deleted, default | |
| 163 | + | |
| 164 | +class B | |
| 165 | +{ | |
| 166 | + public: | |
| 167 | + B(int x) : | |
| 168 | + x(x) | |
| 169 | + { | |
| 170 | + } | |
| 171 | + B() : B(5) | |
| 172 | + { | |
| 173 | + } | |
| 174 | + int getX() const | |
| 175 | + { | |
| 176 | + return x; | |
| 177 | + } | |
| 178 | + | |
| 179 | + virtual ~B() = default; | |
| 180 | + B(B const&) = delete; | |
| 181 | + B& operator=(B const&) = delete; | |
| 182 | + | |
| 183 | + private: | |
| 184 | + int x; | |
| 185 | +}; | |
| 186 | + | |
| 187 | +void do_default_deleted() | |
| 188 | +{ | |
| 189 | + B b1; | |
| 190 | + assert(5 == b1.getX()); | |
| 191 | + assert(std::is_copy_constructible<A>::value); | |
| 192 | + assert(! std::is_copy_constructible<B>::value); | |
| 193 | +} | |
| 194 | + | |
| 195 | +// smart pointers | |
| 196 | + | |
| 197 | +class C | |
| 198 | +{ | |
| 199 | + public: | |
| 200 | + C(int id = 0) : | |
| 201 | + id(id) | |
| 202 | + { | |
| 203 | + incr(id); | |
| 204 | + } | |
| 205 | + ~C() | |
| 206 | + { | |
| 207 | + decr(id); | |
| 208 | + } | |
| 209 | + C(C const& rhs) : C(rhs.id) | |
| 210 | + { | |
| 211 | + } | |
| 212 | + C& operator=(C const& rhs) | |
| 213 | + { | |
| 214 | + if (&rhs != this) | |
| 215 | + { | |
| 216 | + decr(id); | |
| 217 | + id = rhs.id; | |
| 218 | + incr(id); | |
| 219 | + } | |
| 220 | + return *this; | |
| 221 | + } | |
| 222 | + static void check(size_t size, int v, int count) | |
| 223 | + { | |
| 224 | + assert(m.size() == size); | |
| 225 | + auto p = m.find(v); | |
| 226 | + if (p != m.end()) | |
| 227 | + { | |
| 228 | + assert(p->second == count); | |
| 229 | + } | |
| 230 | + } | |
| 231 | + | |
| 232 | + private: | |
| 233 | + void incr(int i) | |
| 234 | + { | |
| 235 | + ++m[i]; | |
| 236 | + } | |
| 237 | + void decr(int i) | |
| 238 | + { | |
| 239 | + if (--m[i] == 0) | |
| 240 | + { | |
| 241 | + m.erase(i); | |
| 242 | + } | |
| 243 | + } | |
| 244 | + | |
| 245 | + static std::map<int, int> m; | |
| 246 | + int id; | |
| 247 | +}; | |
| 248 | + | |
| 249 | +std::map<int, int> C::m; | |
| 250 | + | |
| 251 | +std::shared_ptr<C> make_c(int id) | |
| 252 | +{ | |
| 253 | + return std::make_shared<C>(id); | |
| 254 | +} | |
| 255 | + | |
| 256 | +std::shared_ptr<C> make_c_array(std::vector<int> const& is) | |
| 257 | +{ | |
| 258 | + auto p = std::shared_ptr<C>(new C[is.size()], std::default_delete<C[]>()); | |
| 259 | + C* pp = p.get(); | |
| 260 | + for (size_t i = 0; i < is.size(); ++i) | |
| 261 | + { | |
| 262 | + pp[i] = C(is.at(i)); | |
| 263 | + } | |
| 264 | + return p; | |
| 265 | +} | |
| 266 | + | |
| 267 | +void do_smart_pointers() | |
| 268 | +{ | |
| 269 | + auto p1 = make_c(1); | |
| 270 | + C::check(1, 1, 1); | |
| 271 | + auto p2 = make_c_array({2, 3, 4, 5}); | |
| 272 | + for (auto i: {1, 2, 3, 4, 5}) | |
| 273 | + { | |
| 274 | + C::check(5, i, 1); | |
| 275 | + } | |
| 276 | + { | |
| 277 | + C::check(5, 1, 1); | |
| 278 | + C c3(*p1); | |
| 279 | + C::check(5, 1, 2); | |
| 280 | + } | |
| 281 | + C::check(5, 1, 1); | |
| 282 | + p1 = nullptr; | |
| 283 | + C::check(4, 1, 0); | |
| 284 | + p2 = nullptr; | |
| 285 | + C::check(0, 0, 0); | |
| 286 | + { | |
| 287 | + std::unique_ptr<C> p3(new C(6)); | |
| 288 | + C::check(1, 6, 1); | |
| 289 | + } | |
| 290 | + C::check(0, 0, 0); | |
| 291 | +} | |
| 292 | + | |
| 293 | +int main() | |
| 294 | +{ | |
| 295 | + do_functional(); | |
| 296 | + do_inttypes(); | |
| 297 | + do_iteration(); | |
| 298 | + do_variadic(); | |
| 299 | + do_default_deleted(); | |
| 300 | + do_smart_pointers(); | |
| 301 | + std::cout << "assertions passed\n"; | |
| 302 | + return 0; | |
| 303 | +} | ... | ... |
libtests/qtest/cxx11.test
0 → 100644
| 1 | +#!/usr/bin/env perl | |
| 2 | +require 5.008; | |
| 3 | +use warnings; | |
| 4 | +use strict; | |
| 5 | + | |
| 6 | +require TestDriver; | |
| 7 | + | |
| 8 | +my $td = new TestDriver('c++-11'); | |
| 9 | + | |
| 10 | +$td->runtest("C++-11", | |
| 11 | + {$td->COMMAND => "cxx11"}, | |
| 12 | + {$td->STRING => "assertions passed\n", | |
| 13 | + $td->EXIT_STATUS => 0}, | |
| 14 | + $td->NORMALIZE_NEWLINES); | |
| 15 | + | |
| 16 | +$td->report(1); | ... | ... |