source: mainline/uspace/lib/cpp/src/internal/test/array.cpp@ d93f01a6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d93f01a6 was d93f01a6, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: improved array and vector tests

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <algorithm>
30#include <array>
31#include <initializer_list>
32#include <internal/test/tests.hpp>
33
34namespace std::test
35{
36 bool array_test::run()
37 {
38 start();
39
40 auto check1 = {1, 2, 3, 4};
41 auto check2 = {4, 3, 2, 1};
42 auto check3 = {5, 5, 5, 5};
43
44 std::array<int, 4> arr1{1, 2, 3, 4};
45 test_eq(
46 "initializer list construction",
47 arr1.begin(), arr1.end(),
48 check1.begin(), check1.end()
49 );
50
51 auto it = arr1.begin();
52 test_eq(
53 "iterator increment",
54 *(++it), arr1[1]
55 );
56 test_eq(
57 "iterator decrement",
58 *(--it), arr1[0]
59 );
60
61 std::array<int, 4> arr2{arr1};
62 test_eq(
63 "copy construction",
64 arr1.begin(), arr1.end(),
65 arr2.begin(), arr2.end()
66 );
67
68 std::reverse(arr2.begin(), arr2.end());
69 test_eq(
70 "reverse",
71 arr2.begin(), arr2.end(),
72 check2.begin(), check2.end()
73 );
74 test_eq(
75 "reverse iterator",
76 arr1.rbegin(), arr1.rend(),
77 arr2.begin(), arr2.end()
78 );
79
80
81 std::array<int, 4> arr3{};
82 arr3.fill(5);
83 test_eq(
84 "fill",
85 arr3.begin(), arr3.end(),
86 check3.begin(), check3.end()
87 );
88
89 arr2.swap(arr3);
90 test_eq(
91 "swap part 1",
92 arr2.begin(), arr2.end(),
93 check3.begin(), check3.end()
94 );
95 test_eq(
96 "swap part 2",
97 arr3.begin(), arr3.end(),
98 check2.begin(), check2.end()
99 );
100
101 // TODO: test bound checking of at when implemented
102
103 std::array<int, 3> arr4{1, 2, 3};
104 auto [a, b, c] = arr4;
105 test_eq("structured binding part 1", a, 1);
106 test_eq("structured binding part 2", b, 2);
107 test_eq("structured binding part 3", c, 3);
108
109 return end();
110 }
111
112 const char* array_test::name()
113 {
114 return "array";
115 }
116}
Note: See TracBrowser for help on using the repository browser.