1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 module tanya.container.tests.list;
5 
6 import tanya.container.list;
7 import tanya.test.stub;
8 
9 @nogc nothrow pure @safe unittest
10 {
11     interface Stuff
12     {
13     }
14     static assert(is(SList!Stuff));
15 }
16 
17 @nogc nothrow pure @safe unittest
18 {
19     auto l = SList!int(0, 0);
20     assert(l.empty);
21 }
22 
23 // foreach called using opIndex().
24 @nogc nothrow pure @safe unittest
25 {
26     SList!int l;
27     size_t i;
28 
29     l.insertFront(5);
30     l.insertFront(4);
31     l.insertFront(9);
32     foreach (e; l)
33     {
34         assert(i != 0 || e == 9);
35         assert(i != 1 || e == 4);
36         assert(i != 2 || e == 5);
37         ++i;
38     }
39 }
40 
41 @nogc nothrow pure @safe unittest
42 {
43     auto l1 = SList!int();
44     auto l2 = SList!int([9, 4]);
45     l1 = l2[];
46     assert(l1 == l2);
47 }
48 
49 @nogc nothrow pure @safe unittest
50 {
51     class A
52     {
53     }
54     static assert(is(SList!(A*)));
55     static assert(is(DList!(A*)));
56 }
57 
58 // Removes all elements
59 @nogc nothrow pure @safe unittest
60 {
61     auto l = DList!int([5]);
62     assert(l.remove(l[]).empty);
63 }
64 
65 @nogc nothrow pure @safe unittest
66 {
67     auto l1 = DList!int([5, 234, 30, 1]);
68     auto l2 = DList!int([5, 1]);
69     auto r = l1[];
70 
71     r.popFront();
72     r.popBack();
73     assert(r.front == 234);
74     assert(r.back == 30);
75 
76     assert(!l1.remove(r).empty);
77     assert(l1 == l2);
78 }
79 
80 @nogc nothrow pure @safe unittest
81 {
82     auto l = DList!int(0, 0);
83     assert(l.empty);
84 }
85 
86 @nogc nothrow pure @safe unittest
87 {
88     DList!int l;
89     l.insertAfter(l[], 234);
90     assert(l.front == 234);
91     assert(l.back == 234);
92 }
93 
94 @nogc nothrow pure @safe unittest
95 {
96     auto l1 = DList!int();
97     auto l2 = DList!int([9, 4]);
98     l1 = l2[];
99     assert(l1 == l2);
100 }
101 
102 // Sets the new head
103 @nogc nothrow pure @safe unittest
104 {
105     auto l1 = DList!int([5, 234, 30, 1]);
106     auto l2 = DList!int([1]);
107     auto r = l1[];
108 
109     r.popBack();
110 
111     assert(!l1.remove(r).empty);
112     assert(l1 == l2);
113 }
114 
115 // Can have non-copyable elements
116 @nogc nothrow pure @safe unittest
117 {
118     static assert(is(SList!NonCopyable));
119     static assert(is(DList!NonCopyable));
120 }