1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 
8 #include "cmRange.h"
9 
10 #define ASSERT_TRUE(x)                                                        \
11   do {                                                                        \
12     if (!(x)) {                                                               \
13       std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \
14       return -1;                                                              \
15     }                                                                         \
16   } while (false)
17 
testRange(int,char * [])18 int testRange(int /*unused*/, char* /*unused*/ [])
19 {
20   std::vector<int> const testData = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
21 
22   ASSERT_TRUE(!cmMakeRange(testData).empty());
23   ASSERT_TRUE(cmMakeRange(testData).size() == 10);
24 
25   ASSERT_TRUE(!cmMakeRange(testData).advance(5).empty());
26   ASSERT_TRUE(cmMakeRange(testData).advance(5).size() == 5);
27 
28   ASSERT_TRUE(cmMakeRange(testData).advance(5).retreat(5).empty());
29   ASSERT_TRUE(cmMakeRange(testData).advance(5).retreat(5).size() == 0);
30 
31   ASSERT_TRUE(cmMakeRange(testData).any_of([](int n) { return n % 3 == 0; }));
32   ASSERT_TRUE(cmMakeRange(testData).all_of([](int n) { return n < 11; }));
33   ASSERT_TRUE(cmMakeRange(testData).none_of([](int n) { return n > 11; }));
34 
35   std::vector<int> const evenData = { 2, 4, 6, 8, 10 };
36   ASSERT_TRUE(cmMakeRange(testData).filter([](int n) { return n % 2 == 0; }) ==
37               cmMakeRange(evenData));
38 
39   std::vector<std::string> const stringRange = { "1", "2", "3", "4", "5" };
40   ASSERT_TRUE(cmMakeRange(testData)
41                 .transform([](int n) { return std::to_string(n); })
42                 .retreat(5) == cmMakeRange(stringRange));
43 
44   return 0;
45 }
46