source: mainline/uspace/lib/cpp/src/__bits/test/array.cpp@ 7bbf91e

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

cpp: changed internal to bits to avoid include space pollusion, fixed old std::hel:: bugs in files that weren't touched since

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2018 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 <__bits/test/tests.hpp>
33
34namespace std::test
35{
36 bool array_test::run(bool report)
37 {
38 report_ = report;
39 start();
40
41 auto check1 = {1, 2, 3, 4};
42 auto check2 = {4, 3, 2, 1};
43 auto check3 = {5, 5, 5, 5};
44
45 std::array<int, 4> arr1{1, 2, 3, 4};
46 test_eq(
47 "initializer list construction",
48 arr1.begin(), arr1.end(),
49 check1.begin(), check1.end()
50 );
51
52 auto it = arr1.begin();
53 test_eq(
54 "iterator increment",
55 *(++it), arr1[1]
56 );
57 test_eq(
58 "iterator decrement",
59 *(--it), arr1[0]
60 );
61
62 std::array<int, 4> arr2{arr1};
63 test_eq(
64 "copy construction",
65 arr1.begin(), arr1.end(),
66 arr2.begin(), arr2.end()
67 );
68
69 std::reverse(arr2.begin(), arr2.end());
70 test_eq(
71 "reverse",
72 arr2.begin(), arr2.end(),
73 check2.begin(), check2.end()
74 );
75 test_eq(
76 "reverse iterator",
77 arr1.rbegin(), arr1.rend(),
78 arr2.begin(), arr2.end()
79 );
80
81
82 std::array<int, 4> arr3{};
83 arr3.fill(5);
84 test_eq(
85 "fill",
86 arr3.begin(), arr3.end(),
87 check3.begin(), check3.end()
88 );
89
90 arr2.swap(arr3);
91 test_eq(
92 "swap part 1",
93 arr2.begin(), arr2.end(),
94 check3.begin(), check3.end()
95 );
96 test_eq(
97 "swap part 2",
98 arr3.begin(), arr3.end(),
99 check2.begin(), check2.end()
100 );
101
102 // TODO: test bound checking of at when implemented
103
104 std::array<int, 3> arr4{1, 2, 3};
105 auto [a, b, c] = arr4;
106 test_eq("structured binding part 1", a, 1);
107 test_eq("structured binding part 2", b, 2);
108 test_eq("structured binding part 3", c, 3);
109
110 return end();
111 }
112
113 const char* array_test::name()
114 {
115 return "array";
116 }
117}
Note: See TracBrowser for help on using the repository browser.