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 
36 #include <catch.hpp>
37 
38 #include <string>
39 
40 using std::string;
41 
42 namespace parameterFramework
43 {
44 
45 const auto validFixedPointInstances =
46     Config{&Config::instances,
47            // Default for integers is unsigned/32bits
48            R"(<FixedPointParameter Name="nominal" Size="16" Integral="2" Fractional="7"/>)"};
49 const auto &invalidFixedPointParameters =
50     Tests<string>{{"U8 Dec + Fractional > 7",
51                    "<FixedPointParameter Name='q2.6' Size='8' Integral='2' Fractional='6'/>"},
52                   {"U16 Dec + Fractional > 15",
53                    "<FixedPointParameter Name='q9.7' Size='16' Integral='9' Fractional='7'/>"},
54                   {"U32 Fractional > 31",
55                    "<FixedPointParameter Name='q0.32' Size='32' Integral='0' Fractional='32'/>"}};
56 
57 struct FixedPointPF : public ParameterFramework
58 {
FixedPointPFparameterFramework::FixedPointPF59     FixedPointPF() : ParameterFramework{std::move(validFixedPointInstances)} {}
60 };
61 
62 SCENARIO_METHOD(LazyPF, "Invalid FixedPoint types XML structure", "[FixedPoint types]")
63 {
64     for (auto &vec : invalidFixedPointParameters) {
65         GIVEN ("intentional error: " + vec.title) {
66             create(Config{&Config::instances, vec.payload});
67             THEN ("Start should fail") {
68                 CHECK_THROWS_AS(mPf->start(), Exception);
69             }
70         }
71     }
72 }
73 
74 SCENARIO_METHOD(FixedPointPF, "FixedPoint types", "[FixedPoint types]")
75 {
76     GIVEN ("A valid XML structure file") {
77         THEN ("Start should succeed") {
78             CHECK_NOTHROW(start());
79             REQUIRE_NOTHROW(setTuningMode(true));
80             string path = "/test/test/nominal";
81 
82             AND_THEN ("Set/Get a Fixed Point type parameter in real value space") {
83                 for (auto &vec : Tests<string>{
84                          {"(too high)", "4.0000000"},
85                          {"(too low)", "-4.0078125"},
86                          {"(not a number)", "foobar"},
87                      }) {
88                     GIVEN ("Invalid value " + vec.title) {
89                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
90                     }
91                 }
92                 for (auto &vec : Tests<string>{
93                          {"(upper limit)", "3.9921875"},
94                          {"(lower limit)", "-4.0000000"},
95                          {"(inside range)", "0.0000000"},
96                      }) {
97                     GIVEN ("A valid value " + vec.title) {
98                         CHECK_NOTHROW(setParameter(path, vec.payload));
99                         string getValueBack;
100                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
101                         CHECK(getValueBack == vec.payload);
102                     }
103                 }
104             }
105             AND_THEN ("Set/Get a Fixed Point parameter in raw value space") {
106                 REQUIRE_NOTHROW(setRawValueSpace(true));
107 
108                 for (auto &vec : Tests<string>{
109                          {"(upper limit) as decimal", "32767"},
110                          {"(lower limit) as decimal", "-32768"},
111                          {"(inside range) as decimal", "0"},
112                      }) {
113                     GIVEN ("A valid value " + vec.title) {
114                         CHECK_NOTHROW(setParameter(path, vec.payload));
115                         string getValueBack;
116                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
117                         CHECK(getValueBack == vec.payload);
118                     }
119                 }
120                 for (auto &vec : Tests<string>{
121                          {"(too high)", "32768"}, {"(too low)", "-32769"},
122                      }) {
123                     GIVEN ("Invalid value interger" + vec.title) {
124                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
125                     }
126                 }
127                 for (auto &vec : Tests<string>{
128                          {"(too high)", "0x10000"}, {"(too low)", "0xfffe0000"},
129                      }) {
130                     GIVEN ("Invalid value hexa" + vec.title) {
131                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
132                     }
133                 }
134             }
135         }
136     }
137 }
138 }
139