• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "Config.hpp"
32 #include "ParameterFramework.hpp"
33 #include "ElementHandle.hpp"
34 #include "Test.hpp"
35 #include "BinaryCopy.hpp"
36 
37 #include <catch.hpp>
38 
39 #include <string>
40 
41 using std::string;
42 
43 namespace parameterFramework
44 {
45 
46 const auto validLinearInstances = Config{
47     &Config::instances,
48     // Size is fixed at 32 and as such is optional */
49     R"(<IntegerParameter Name="trivial" Signed="true"> <LinearAdaptation SlopeNumerator="200" SlopeDenominator="10"/> </IntegerParameter>
50     <IntegerParameter Name="nominal" Size="32" Signed="true" Min="-1440" Max="300"> <LinearAdaptation SlopeNumerator="10" SlopeDenominator="100"/> </IntegerParameter>)"};
51 const auto &invalidLinearParameters = Tests<string>{
52     {"invalid Size(64)", "<IntegerParameter Name='nominal' Size='64' Signed='true' Min='-144' "
53                          "Max='30'> <LinearAdaptation SlopeNumerator='200' SlopeDenominator='10'/> "
54                          "</IntegerParameter>"},
55     {"minimum > maximum", "<IntegerParameter Name='nominal' Size='32' Signed='true' Min='30' "
56                           "Max='-144'> <LinearAdaptation SlopeNumerator='1' "
57                           "SlopeDenominator='10'/> </IntegerParameter>"},
58     {"SlopeDenominator=0",
59      "<IntegerParameter Name='nominal' Size='32' Signed='true' Min='-144' Max='30'> "
60      "<LinearAdaptation SlopeNumerator='1' SlopeDenominator='0'/> "
61      "</IntegerParameter>"}};
62 
63 struct LinearsPF : public ParameterFramework
64 {
LinearsPFparameterFramework::LinearsPF65     LinearsPF() : ParameterFramework{std::move(validLinearInstances)} {}
66 };
67 
68 SCENARIO_METHOD(LazyPF, "Invalid Linear points XML structure", "[Linear Type]")
69 {
70     for (auto &vec : invalidLinearParameters) {
71         GIVEN ("intentional error: " + vec.title) {
72             create(Config{&Config::instances, vec.payload});
73             THEN ("Start should fail") {
74                 CHECK_THROWS_AS(mPf->start(), Exception);
75             }
76         }
77     }
78 }
79 
80 SCENARIO_METHOD(LinearsPF, "Linear points", "[Linear Type]")
81 {
82     GIVEN ("A valid XML structure file") {
83         THEN ("Start should succeed") {
84             CHECK_NOTHROW(start());
85             REQUIRE_NOTHROW(setTuningMode(true));
86             string path = "/test/test/nominal";
87             AND_THEN ("Set/Get a Loagaritmic point parameter in real value space") {
88 
89                 for (auto &vec : Tests<string>{
90                          {"(too high)", "301"},
91                          {"(too low)", "-1441"},
92                          {"(not a number)", "foobar"},
93                      }) {
94                     GIVEN ("Invalid value " + vec.title) {
95                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
96                     }
97                 }
98                 for (auto &vec : Tests<string>{
99                          {"(upper limit)", "300"},
100                          {"(lower limit)", "-1440"},
101                          {"(inside range)", "0"},
102                      }) {
103                     GIVEN ("A valid value " + vec.title) {
104                         CHECK_NOTHROW(setParameter(path, vec.payload));
105                         string getValueBack = "-11";
106                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
107                         CHECK(getValueBack == vec.payload);
108                     }
109                 }
110             }
111 
112             AND_THEN ("Set/Get double type parameter handle") {
113                 ElementHandle handle{*this, path};
114                 /** @FIXME: 'set' operations on a ParameterHandle are silently
115                  * ignored in tuning mode. Does it make sense ? */
116                 REQUIRE_NOTHROW(setTuningMode(false));
117                 REQUIRE_NOTHROW(setRawValueSpace(true));
118 
119                 /* nominal is defined as a Num=10,Denum=100 (division by 10). So limits should be 10
120                  * above Mina dn Max Integer value in order to get exception. */
121                 for (auto &vec : Tests<double>{
122                          {"(upper limit)", 3000.0f},
123                          {"(lower limit)", -14400.0f},
124                          {"(inside range)", 0.0f},
125                      }) {
126                     GIVEN ("A valid value " + vec.title) {
127                         CHECK_NOTHROW(handle.setAsDouble(vec.payload));
128                         double getValueBack = 1.0f;
129                         REQUIRE_NOTHROW(handle.getAsDouble(getValueBack));
130                         CHECK(getValueBack == vec.payload);
131                     }
132                 }
133                 for (auto &vec : Tests<double>{
134                          {"(too high)", 3010.0f}, {"(too low)", -14410.00f},
135                      }) {
136                     GIVEN ("An invalid value " + vec.title) {
137                         CHECK_THROWS_AS(handle.setAsDouble(vec.payload), Exception);
138                     }
139                 }
140             }
141         }
142     }
143 }
144 } // namespace parameterFramework
145