Lolly 1.4.28
Loading...
Searching...
No Matches
array_test.cpp
Go to the documentation of this file.
1#include "a_lolly_test.hpp"
2#include "array.hpp"
3#include "string.hpp"
4
5static array<int>
6gen_array (int n) {
7 auto normal= array<int> ();
8 for (auto i= 1; i <= n; i++) {
9 normal << i;
10 }
11 return normal;
12}
13
16auto two_elem = array<int> (1, 2);
17auto three_elem= array<int> (1, 2, 3);
18auto four_elem = array<int> (1, 2, 3, 4);
19auto five_elem = array<int> (1, 2, 3, 4, 5);
20
21TEST_CASE ("test access") {
22 CHECK_EQ (five_elem[0], 1);
23 CHECK_EQ (five_elem[1], 2);
24 CHECK_EQ (five_elem[2], 3);
25 CHECK_EQ (five_elem[3], 4);
26 CHECK_EQ (five_elem[4], 5);
27 CHECK_EQ (one_elem[0], 1);
28}
29
30TEST_CASE ("test multiply") {
31 auto mulu4= array<int> ();
32 for (auto i= 1; i < 6; i++) {
33 mulu4 << (i * 4);
34 }
36}
37
38TEST_CASE ("test divide") {
39 auto mulu4= array<int> ();
40 for (auto i= 1; i < 6; i++) {
41 mulu4 << (i * 4);
42 }
44}
45
46TEST_CASE ("test range") {
47 CHECK_EQ (range (gen_array (10), 0, 5), five_elem);
49}
50
51TEST_CASE ("test size") {
52 CHECK_EQ (N (zero_elem), 0);
53 CHECK_EQ (N (one_elem), 1);
54 CHECK_EQ (N (two_elem), 2);
55 CHECK_EQ (N (three_elem), 3);
56 CHECK_EQ (N (four_elem), 4);
57 CHECK_EQ (N (five_elem), 5);
58 for (auto i= 6; i < 200; i++) {
59 auto array_test= gen_array (i);
60 CHECK_EQ (N (array_test), i);
61 }
62}
63
72
73TEST_CASE ("test append") {
80
81 auto one2ten= gen_array (10);
82 auto six2ten= array<int> (6, 7, 8, 9, 10);
83 for (auto i= 1; i <= 10; i++) {
84 CHECK_EQ (one2ten[i - 1], i);
85 }
87}
88
89TEST_CASE ("test reverse") {
92 auto rev_five= array<int> (5, 4, 3, 2, 1);
94}
95
96TEST_CASE ("test contains") {
97 CHECK_EQ (contains (1, zero_elem), false);
98 CHECK_EQ (contains (1, one_elem), true);
99 CHECK_EQ (contains (3, two_elem), false);
100 CHECK_EQ (contains (1, five_elem), true);
101 CHECK_EQ (contains (2, five_elem), true);
102 CHECK_EQ (contains (3, five_elem), true);
103}
104
105TEST_CASE ("array of string") {
106 auto arr= array<string> ();
107 arr << "Hello"
108 << "1"
109 << "2"
110 << "3";
111 CHECK (contains (string ("Hello"), arr));
112 arr << "】";
113 CHECK (contains (string ("】"), arr));
114}
115
116TEST_CASE ("test iteration") {
117 int expectedIndex= 0;
118 for (const auto element : five_elem) {
121 }
123
124 expectedIndex= 0;
125 for (const auto element : zero_elem) {
128 }
130}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
array< T > reverse(array< T > a)
Reverse an array.
Definition array.ipp:195
array< T > range(array< T > a, int i, int j)
Get a subarray of an array.
Definition array.ipp:184
bool contains(T a, array< T > b)
Check if an array contains a specified element.
array< T > append(T a, array< T > b)
Append an element to the beginning of an array.
auto zero_elem
auto two_elem
auto one_elem
auto five_elem
auto four_elem
static array< int > gen_array(int n)
Definition array_test.cpp:6
auto three_elem
int copy(int x)
Returns a copy of an integer.
Definition basic.hpp:249
The list class represents a linked list.
Definition list.hpp:48
auto normal
Definition list_test.cpp:26
TEST_CASE("test for operator+= and advance()")