OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Enumerate_test.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 class EnumerateUnitTest : public UnitTest
27 {
28 public:
29  EnumerateUnitTest() : UnitTest ("Enumerate", UnitTestCategories::containers) {}
30 
31  void runTest() override
32  {
33  beginTest ("enumeration works for bidirectional iterators");
34  {
35  const std::list<int> elements { 10, 20, 30, 40, 50 };
36  std::vector<int> counts;
37 
38  for (const auto pair : enumerate (elements))
39  counts.push_back ((int) pair.index);
40 
41  expect (counts == std::vector<int> { 0, 1, 2, 3, 4 });
42  }
43 
44  beginTest ("enumeration works for random-access iterators");
45  {
46  const std::vector<std::string> strings { "a", "bb", "ccc", "dddd", "eeeee" };
47 
48  std::vector<int> counts;
49 
50  for (const auto [count, element] : enumerate (strings))
51  counts.push_back ((int) ((size_t) count + element.size()));
52 
53  expect (counts == std::vector<int> { 1, 3, 5, 7, 9 });
54  }
55 
56  beginTest ("enumeration works for mutable ranges");
57  {
58  std::vector<std::string> strings { "", "", "", "", "" };
59 
60  for (const auto [count, element] : enumerate (strings))
61  element = std::to_string (count);
62 
63  expect (strings == std::vector<std::string> { "0", "1", "2", "3", "4" });
64  }
65 
66  beginTest ("iterator can be incremented by more than one");
67  {
68  std::vector<int> ints (6);
69 
70  const auto enumerated = enumerate (ints);
71 
72  std::vector<int> counts;
73 
74  for (auto b = enumerated.begin(), e = enumerated.end(); b != e; b += 2)
75  counts.push_back ((int) (*b).index);
76 
77  expect (counts == std::vector<int> { 0, 2, 4 });
78  }
79 
80  beginTest ("iterator can be started at a non-zero value");
81  {
82  const std::vector<int> ints (6);
83 
84  std::vector<int> counts;
85 
86  for (const auto enumerated : enumerate (ints, 5))
87  counts.push_back ((int) enumerated.index);
88 
89  expect (counts == std::vector<int> { 5, 6, 7, 8, 9, 10 });
90  }
91 
92  beginTest ("subtracting two EnumerateIterators returns the difference between the base iterators");
93  {
94  const std::vector<int> ints (6);
95  const auto enumerated = enumerate (ints);
96  expect ((int) (enumerated.end() - enumerated.begin()) == (int) ints.size());
97  }
98 
99  beginTest ("EnumerateIterator can be decremented");
100  {
101  const std::vector<int> ints (5);
102  std::vector<int> counts;
103 
104  const auto enumerated = enumerate (std::as_const (ints));
105 
106  for (auto i = enumerated.end(), b = enumerated.begin(); i != b; --i)
107  counts.push_back ((int) (*(i - 1)).index);
108 
109  expect (counts == std::vector<int> { -1, -2, -3, -4, -5 });
110  }
111 
112  beginTest ("EnumerateIterator can be compared");
113  {
114  const std::vector<int> ints (6);
115  const auto enumerated = enumerate (ints);
116  expect (enumerated.begin() < enumerated.end());
117  expect (enumerated.begin() <= enumerated.end());
118  expect (enumerated.end() > enumerated.begin());
119  expect (enumerated.end() >= enumerated.begin());
120  }
121  }
122 };
123 
124 static EnumerateUnitTest enumerateUnitTest;
125 
126 } // namespace juce
UnitTest(const String &name, const String &category=String())
void beginTest(const String &testName)
void expect(bool testResult, const String &failureMessage=String())