Belle II Software development
numberSequence.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/utilities/NumberSequence.h>
9#include <gtest/gtest.h>
10
11using namespace Belle2;
12namespace {
13
14 TEST(NumberSequenceTest, TestSingleNumber)
15 {
16 auto sequence = generate_number_sequence("3");
17 std::set<int64_t> expected = {3};
18 EXPECT_EQ(sequence, expected);
19 }
20
21 TEST(NumberSequenceTest, TestManyNumbers)
22 {
23 auto sequence = generate_number_sequence("3,4,5,10,12");
24 std::set<int64_t> expected = {3, 4, 5, 10, 12};
25 EXPECT_EQ(sequence, expected);
26 }
27
28 TEST(NumberSequenceTest, TestNegativeNumbers)
29 {
30 auto sequence = generate_number_sequence("-3,4,-5,10,12");
31 std::set<int64_t> expected = { -5, -3, 4, 10, 12};
32 EXPECT_EQ(sequence, expected);
33 }
34
35 TEST(NumberSequenceTest, TestOutOfOrderNumbers)
36 {
37 auto sequence = generate_number_sequence("3,10,4,12,5");
38 std::set<int64_t> expected = {3, 4, 5, 10, 12};
39 EXPECT_EQ(sequence, expected);
40 }
41
42 TEST(NumberSequenceTest, TestSingleInterval)
43 {
44 auto sequence = generate_number_sequence("3:6");
45 std::set<int64_t> expected = {3, 4, 5, 6};
46 EXPECT_EQ(sequence, expected);
47 }
48
49 TEST(NumberSequenceTest, TestManyIntervals)
50 {
51 auto sequence = generate_number_sequence("3:6,7:9,12:13");
52 std::set<int64_t> expected = {3, 4, 5, 6, 7, 8, 9, 12, 13};
53 EXPECT_EQ(sequence, expected);
54 }
55
56 TEST(NumberSequenceTest, TestOutOfOrderIntervals)
57 {
58 auto sequence = generate_number_sequence("12:13,3:6,7:9");
59 std::set<int64_t> expected = {3, 4, 5, 6, 7, 8, 9, 12, 13};
60 EXPECT_EQ(sequence, expected);
61 }
62
63 TEST(NumberSequenceTest, TestOverlappingIntervals)
64 {
65 auto sequence = generate_number_sequence("3:6,5:7,6:8");
66 std::set<int64_t> expected = {3, 4, 5, 6, 7, 8};
67 EXPECT_EQ(sequence, expected);
68 }
69
70 TEST(NumberSequenceTest, TestNegativeIntervals)
71 {
72 auto sequence = generate_number_sequence("3:6,-5:7,6:8");
73 std::set<int64_t> expected = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8};
74 EXPECT_EQ(sequence, expected);
75 }
76
77 TEST(NumberSequenceTest, TestReversedIntervalIsEmpty)
78 {
79 auto sequence = generate_number_sequence("8:6");
80 std::set<int64_t> expected;
81 EXPECT_EQ(sequence, expected);
82 }
83
84 TEST(NumberSequenceTest, TestMixNumbersAndIntervals)
85 {
86 auto sequence = generate_number_sequence("1,12:13,3,6,7:9,12");
87 std::set<int64_t> expected = {1, 3, 6, 7, 8, 9, 12, 13};
88 EXPECT_EQ(sequence, expected);
89 }
90
91 TEST(NumberSequenceTest, TestWhitespaces)
92 {
93 auto sequence = generate_number_sequence("1 , 12 : 13 ,3 ,6 ,7 :9,12 ");
94 std::set<int64_t> expected = {1, 3, 6, 7, 8, 9, 12, 13};
95 EXPECT_EQ(sequence, expected);
96 }
97
98} // namespace
std::set< int64_t > generate_number_sequence(const std::string &str)
Generate a sequence of numbers defined by a string.
Abstract base class for different kinds of events.