File: FunctionalTests.cpp

package info (click to toggle)
opm-common 2024.10%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 98,420 kB
  • sloc: cpp: 263,013; python: 3,155; sh: 198; xml: 174; pascal: 136; makefile: 12
file content (123 lines) | stat: -rw-r--r-- 3,754 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
/*
  Copyright 2016 Statoil ASA.

  This file is part of the Open Porous Media project (OPM).

  OPM is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  OPM is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with OPM.  If not, see <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE FunctionalTests

#include <iostream>
#include <vector>
#include <map>

#include <boost/test/unit_test.hpp>

#include <opm/input/eclipse/Utility/Functional.hpp>


using namespace Opm;


BOOST_AUTO_TEST_CASE(TestMap) {
    std::map<std::string, int> m = { {"C", 3}, {"B" , 2} , {"A" , 1}};
    std::vector<std::string> keys_expected = {"A" , "B" , "C"};
    auto keys = fun::map( [] ( const std::pair<std::string,int>& pair) { return pair.first; } , m);

    BOOST_CHECK_EQUAL_COLLECTIONS(keys.begin(), keys.end(),
                                  keys_expected.begin(), keys_expected.end());
}


BOOST_AUTO_TEST_CASE(TestConcat) {
    std::vector<std::vector<int>> vector_of_vectors = {{1},{2,2},{3,3,3}};
    auto conc = fun::concat( std::move(vector_of_vectors) );
    std::vector<int> expected = {1,2,2,3,3,3};

    BOOST_CHECK_EQUAL_COLLECTIONS(conc.begin(), conc.end(),
                                  expected.begin(), expected.end());
}


BOOST_AUTO_TEST_CASE(TestConcatMap) {
    std::vector<int> input = {1,2,3};
    auto conc = fun::concat( fun::map( []( int x ) { return std::vector<int>( x,x ); } , input));

    std::vector<int> expected = {1,2,2,3,3,3};
    BOOST_CHECK_EQUAL_COLLECTIONS(conc.begin(), conc.end(),
                                  expected.begin(), expected.end());

}



BOOST_AUTO_TEST_CASE(iotaEqualCollections) {
    std::vector< int > vec( 5 );

    for( int i = 0; i < 5; ++i )
        vec[ i ] = i;

    fun::iota iota( 5 );
    for( auto x : iota )
        std::cout << x << " ";
    std::cout << std::endl;
    std::vector< int > vec_iota( iota.begin(), iota.end() );

    BOOST_CHECK_EQUAL_COLLECTIONS(
            vec_iota.begin(), vec_iota.end(),
            vec.begin(), vec.end() );
    BOOST_CHECK_EQUAL_COLLECTIONS(
            vec_iota.begin(), vec_iota.end(),
            fun::iota( 5 ).begin(), fun::iota( 5 ).end() );
    BOOST_CHECK_EQUAL_COLLECTIONS(
            vec.begin(), vec.end(),
            fun::iota( 5 ).begin(), fun::iota( 5 ).end() );
}

BOOST_AUTO_TEST_CASE(iotaForeach) {
    /* this test is mostly a syntax verification test */

    std::vector< int > vec = { 0, 1, 2, 3, 4 };

    for( auto x : fun::iota( 5 ) )
        BOOST_CHECK_EQUAL( vec[ x ], x );
}

BOOST_AUTO_TEST_CASE(iotaSize) {
    BOOST_CHECK_EQUAL( 5U, fun::iota( 5 ).size() );
    BOOST_CHECK_EQUAL( 5U, fun::iota( 1, 6 ).size() );
    BOOST_CHECK_EQUAL( 0U, fun::iota( 0 ).size() );
    BOOST_CHECK_EQUAL( 0U, fun::iota( 0, 0 ).size() );
}

BOOST_AUTO_TEST_CASE(iotaWithMap) {
    const auto plus1 = []( int x ) { return x + 1; };

    std::vector< int > vec = { 1, 2, 3, 4, 5 };
    auto vec_iota = fun::map( plus1, fun::iota( 5 ) );

    BOOST_CHECK_EQUAL_COLLECTIONS(
            vec_iota.begin(), vec_iota.end(),
            vec.begin(), vec.end() );
}

BOOST_AUTO_TEST_CASE(iotaNegativeBegin) {
    const auto vec = { -4, -3, -2, -1, 0 };

    fun::iota iota( -4, 1 );

    BOOST_CHECK_EQUAL_COLLECTIONS(
            vec.begin(), vec.end(),
            iota.begin(), iota.end() );
}