File: Test_List.cpp

package info (click to toggle)
pymol 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,084 kB
  • sloc: cpp: 482,660; python: 89,328; ansic: 29,512; javascript: 6,792; sh: 84; makefile: 25
file content (203 lines) | stat: -rw-r--r-- 4,845 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "List.h"
#include "ListMacros.h"
#include "Test.h"

#include <numeric>

struct Foo {
  int someInt;
  Foo* next;
};

/**
 * Mimics ListElemCalloc without PyMOLGlobals
 * @return a zero-initialized instance on the free store
 */
template <typename T> static T* ListElemCallocNoG()
{
  static_assert(std::is_trivial<T>::value, "");
  return pymol::calloc<T>(1);
}

TEST_CASE("Legacy List", "[List]")
{
  Foo* myList;
  ListInit(myList);
  for (int i = 0; i < 4; ++i) {
    auto bar = ListElemCallocNoG<Foo>();
    bar->someInt = i;
    ListAppend(myList, bar, next, Foo);
  }

  Foo* fooPtr = nullptr;
  int count = 0;
  while (ListIterate(myList, fooPtr, next)) {
    count += fooPtr->someInt;
  }
  REQUIRE(count == 6);
  ListFree(myList, next, Foo);
}


TEST_CASE("pymol::ListAdapter type checks", "[List]")
{
  Foo* myList;
  ListInit(myList);
  auto myAdapter = pymol::make_list_adapter(myList);
  static_assert(
      std::is_same<decltype(myAdapter.begin())::value_type, Foo>::value,
      "auto begin wrong type");
  static_assert(
      std::is_same<decltype(myAdapter.cbegin())::value_type, const Foo>::value,
      "auto cbegin wrong type");
  static_assert(std::is_same<decltype(myAdapter.end())::value_type, Foo>::value,
      "auto end wrong type");
  static_assert(
      std::is_same<decltype(myAdapter.cend())::value_type, const Foo>::value,
      "auto cend wrong type");
  ListFree(myList, next, Foo);
}

TEST_CASE("pymol::ListAdapter For pre", "[List]")
{
  Foo* myList;
  ListInit(myList);
  for (int i = 0; i < 4; ++i) {
    auto bar = ListElemCallocNoG<Foo>();
    bar->someInt = i;
    ListAppend(myList, bar, next, Foo);
  }

  auto myAdapter = pymol::make_list_adapter(myList);

  int count = 0;
  for (auto it = myAdapter.begin(); it != myAdapter.end(); ++it) {
    count += it->someInt;
  }
  REQUIRE(count == 6);
  ListFree(myList, next, Foo);
}

TEST_CASE("pymol::ListAdapter For post", "[List]")
{
  Foo* myList;
  ListInit(myList);
  for (int i = 0; i < 4; ++i) {
    auto bar = ListElemCallocNoG<Foo>();
    bar->someInt = i;
    ListAppend(myList, bar, next, Foo);
  }

  auto myAdapter = pymol::make_list_adapter(myList);

  int count = 0;
  for (auto it = myAdapter.begin(); it != myAdapter.end(); it++) {
    count += it->someInt;
  }
  REQUIRE(count == 6);
  ListFree(myList, next, Foo);
}

TEST_CASE("pymol::ListAdapter For cbegin", "[List]")
{
  Foo* myList;
  ListInit(myList);
  for (int i = 0; i < 4; ++i) {
    auto bar = ListElemCallocNoG<Foo>();
    bar->someInt = i;
    ListAppend(myList, bar, next, Foo);
  }

  auto myAdapter = pymol::make_list_adapter(myList);

  int count = 0;
  for (auto it = myAdapter.cbegin(); it != myAdapter.cend(); it++) {
    count += it->someInt;
  }
  REQUIRE(count == 6);

  ListFree(myList, next, Foo);
}

TEST_CASE("pymol::ListAdapter For const_iterator", "[List]")
{
  Foo* myList;
  ListInit(myList);
  for (int i = 0; i < 4; ++i) {
    auto bar = ListElemCallocNoG<Foo>();
    bar->someInt = i;
    ListAppend(myList, bar, next, Foo);
  }

  auto myAdapter = pymol::make_list_adapter(myList);

  int count = 0;
  for (pymol::ListAdapter<Foo>::const_iterator it = myAdapter.begin();
       it != myAdapter.end(); it++) {
    count += it->someInt;
  }
  REQUIRE(count == 6);
  ListFree(myList, next, Foo);
}

TEST_CASE("pymol::ListAdapter Ranged For", "[List]")
{
  Foo* myList;
  ListInit(myList);
  for (int i = 0; i < 4; ++i) {
    auto bar = ListElemCallocNoG<Foo>();
    bar->someInt = i;
    ListAppend(myList, bar, next, Foo);
  }

  int count = 0;
  for (const auto& foo : pymol::ListAdapter<Foo>(myList)) {
    count += foo.someInt;
  }
  REQUIRE(count == 6);
  ListFree(myList, next, Foo);
}

TEST_CASE("pymol::ListAdapter Std Algorithm InputIterator", "[List]")
{
  Foo* myList;
  ListInit(myList);
  for (int i = 0; i < 4; ++i) {
    auto bar = ListElemCallocNoG<Foo>();
    bar->someInt = i;
    ListAppend(myList, bar, next, Foo);
  }

  auto myAdapter = pymol::make_list_adapter(myList);

  int count = std::accumulate(myAdapter.begin(), myAdapter.end(), 0,
      [](int sum, const Foo& b) { return sum + b.someInt; });
  REQUIRE(count == 6);
  ListFree(myList, next, Foo);
}

TEST_CASE("pymol::ListAdapter Std Algorithm OutputIterator", "[List]")
{
  Foo* myList;
  ListInit(myList);
  for (int i = 0; i < 4; ++i) {
    auto bar = ListElemCallocNoG<Foo>();
    bar->someInt = i;
    ListAppend(myList, bar, next, Foo);
  }

  auto myAdapter = pymol::make_list_adapter(myList);

  std::transform(
      myAdapter.begin(), myAdapter.end(), myAdapter.begin(), [](Foo& foo) {
        foo.someInt += 1;
        return foo;
      });

  int count = std::accumulate(myAdapter.begin(), myAdapter.end(), 0,
      [](int sum, const Foo& b) { return sum + b.someInt; });
  REQUIRE(count == 10);
  ListFree(myList, next, Foo);
}

// vi:sw=2:expandtab