Project
Loading...
Searching...
No Matches
test_RangeTokenizer.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12// @file test_RangeTokenizer.cxx
13// @author Matthias Richter
14// @since 2018-12-11
15// @brief Test program for RangeTokenizer
16
17#define BOOST_TEST_MODULE Algorithm RangeTokenizer test
18#define BOOST_TEST_MAIN
19#define BOOST_TEST_DYN_LINK
20#include <boost/test/unit_test.hpp>
21#include "../include/Algorithm/RangeTokenizer.h"
22#include <vector>
23#include <map>
24
26
27BOOST_AUTO_TEST_CASE(test_simple_integral)
28{
29 // the simple case using integral type
30 std::vector<int> tokens = RangeTokenizer::tokenize<int>("0-5,10,13-15");
31 std::vector<int> expected{0, 1, 2, 3, 4, 5, 10, 13, 14, 15};
32 BOOST_CHECK(tokens == expected);
33}
34
35BOOST_AUTO_TEST_CASE(test_simple_string)
36{
37 // simple case using string type
38 std::vector<std::string> tokens = RangeTokenizer::tokenize<std::string>("apple,strawberry,tomato");
39 BOOST_CHECK(tokens[0] == "apple");
40 BOOST_CHECK(tokens[1] == "strawberry");
41 BOOST_CHECK(tokens[2] == "tomato");
42}
43
44BOOST_AUTO_TEST_CASE(test_mapped_custom)
45{
46 // process a custom type according to a map
47 enum struct Food { Apple,
48 Strawberry,
49 Tomato };
50
51 const std::map<std::string, Food> FoodMap{
52 {"apple", Food::Apple},
53 {"strawberry", Food::Strawberry},
54 {"tomato", Food::Tomato},
55 };
56 auto tester = [FoodMap](const char* arg) {
57 // use a custom mapper function, this evetually throws an exception if the token is not in the map
58 return std::move(RangeTokenizer::tokenize<Food>(arg, [FoodMap](auto const& token) { return FoodMap.at(token); }));
59 };
60
61 auto tokens = tester("apple,tomato");
62 BOOST_CHECK(tokens[0] == Food::Apple);
63 BOOST_CHECK(tokens[1] == Food::Tomato);
64
65 BOOST_CHECK_THROW(tester("blueberry"), std::out_of_range);
66}
Tokenize a string according to delimiter ',' and extract values of type T.
std::map< std::string, ID > expected
BOOST_AUTO_TEST_CASE(test_simple_integral)
BOOST_CHECK(tree)