Lolly 1.4.28
Loading...
Searching...
No Matches
list_test.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : list_test.cpp
4 * DESCRIPTION: test on linked lists with reference counting
5 * COPYRIGHT : (C) 2018 Darcy Shen
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "a_lolly_test.hpp"
13#include "list.hpp"
14#include "string.hpp"
15
16static list<long>
18 auto normal= list<long> ();
19 for (long i= 0; i < n; i++)
20 normal << i;
21 return normal;
22}
23
26auto normal = list<long> (1, 2, 3, list<long> ());
27
28TEST_CASE ("is nil") {
30 CHECK_EQ (is_nil (the_atom_list), false);
31}
32
33TEST_CASE ("is atom") {
34 CHECK_EQ (is_atom (the_nil_list), false);
36}
37
38/******************************************************************************
39 * tests on output and convertion
40 ******************************************************************************/
41
42TEST_CASE ("access") {
43 // QVERIFY_EXCEPTION_THROWN (the_nil_list[0], string);
44
45 CHECK_EQ (the_atom_list[0], 1L);
46
47 CHECK_EQ (normal[0], 1L);
48 CHECK_EQ (normal[1], 2L);
49 CHECK_EQ (normal[2], 3L);
50}
51
52/******************************************************************************
53 * tests on insertion and suppression
54 ******************************************************************************/
55
56TEST_CASE ("operate on the last") {
57 // QVERIFY_EXCEPTION_THROWN (access_last (the_nil_list), string);
58 // QVERIFY_EXCEPTION_THROWN (suppress_last (the_nil_list), string);
59 // QVERIFY_EXCEPTION_THROWN (last_item (the_nil_list), string);
60
67
68 auto normal_copy = copy (normal);
74}
75
76/******************************************************************************
77 * tests on computations with list<T> structures
78 ******************************************************************************/
79
80TEST_CASE ("size") {
81 CHECK_EQ (N (the_nil_list), 0);
83 CHECK_EQ (N (normal), 3);
84 for (auto i= 4; i <= 100; i++) {
85 auto list= gen (i);
86 CHECK_EQ (N (list), i);
87 }
88}
89
95
96TEST_CASE ("append") {
97 auto appended= the_nil_list * string ("a");
98 CHECK_EQ (appended, list<string> (string ("a")));
99
100 auto to_append= list<string> ("a");
102
105
107
108 CHECK_EQ (list<long> (1L, 2L, list<long> ()) * list<long> (3L), normal);
109 CHECK_EQ (list<long> (1L) * list<long> (2L, 3L, list<long> ()), normal);
110}
111
112TEST_CASE ("head and tail") {
113 // QVERIFY_EXCEPTION_THROWN (head (the_nil_list), string);
114 // QVERIFY_EXCEPTION_THROWN (tail (the_nil_list), string);
115
118
120 CHECK_EQ (tail (normal), list<long> (2, 3, list<long> ()));
121}
122
128
129TEST_CASE ("remove") {
130 CHECK_EQ (remove (the_nil_list, string ("a")), the_nil_list);
132 CHECK_EQ (remove (normal, 2L), list<long> (1, 3, list<long> ()));
133}
134
135TEST_CASE ("contains") {
136 CHECK_EQ (contains (the_nil_list, string ("a")), false);
137 CHECK_EQ (contains (the_atom_list, 1L), true);
138 CHECK_EQ (contains (normal, 1L), true);
139
140 CHECK_EQ (contains (normal, 2L), true);
141 CHECK_EQ (contains (normal, 3L), true);
142}
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
bool contains(T a, array< T > b)
Check if an array contains a specified element.
int copy(int x)
Returns a copy of an integer.
Definition basic.hpp:249
bool is_nil(blackbox x)
Definition blackbox.hpp:29
The list class represents a linked list.
Definition list.hpp:48
list< T > remove(list< T > l, T what)
Create a new list with a specific item removed.
Definition list.ipp:214
list< T > tail(list< T > l, int n=1)
Get all but the first n items of a list.
Definition list.ipp:193
list< T > head(list< T > l, int n=1)
Get the first n items of a list.
Definition list.ipp:185
bool is_atom(list< T > l)
Check if a list is an atom (i.e., a single item).
Definition list.hpp:146
T last_item(list< T > l)
Return the last item of the list. The input list must not be an empty list.
Definition list.ipp:79
T & access_last(list< T > &l)
Get a reference to the last item in a list.
Definition list.ipp:89
list< T > & suppress_last(list< T > &l)
Remove the last item from a list.
Definition list.ipp:97
static list< long > gen(int64_t n)
Definition list_test.cpp:17
auto the_nil_list
Definition list_test.cpp:24
auto the_atom_list
Definition list_test.cpp:25
auto normal
Definition list_test.cpp:26
TEST_CASE("test for operator+= and advance()")