Home | History | Annotate | Download | only in functional-tests
      1 /*
      2  * Copyright (c) 2016, 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 validIntegerInstances = Config{&Config::instances,
     47                                           // Default for integers is unsigned/32bits
     48                                           R"(<IntegerParameter Name="Empty"/>
     49     <IntegerParameter Name="trivial" Size="8" Signed="true"/>
     50     <IntegerParameter Name="nominal" Size="8" Signed="true" Min="-50" Max="12"/>
     51     <IntegerParameter Name="negMinMaxS8" Size="8" Signed="true" Min="-120" Max="-110"/>
     52     <IntegerParameter Name="posMinMaxS8" Size="8" Signed="true" Min="90" Max="100"/>
     53     <IntegerParameter Name="defaultMinS8" Size="8" Signed="true" Min="-128"/>
     54     <IntegerParameter Name="defaultMaxS8" Size="8" Signed="true" Max="127"/>
     55     <IntegerParameter Name="defaultMinU8" Size="8" Signed="false" Min="0"/>
     56     <IntegerParameter Name="defaultMaxU8" Size="8" Signed="false" Max="255"/>
     57     <IntegerParameter Name="defaultMinS16" Size="16" Signed="true" Min="-32768"/>
     58     <IntegerParameter Name="defaultMaxS16" Size="16" Signed="true" Max="32767"/>
     59     <IntegerParameter Name="defaultMinU16" Size="16" Signed="false" Min="0"/>
     60     <IntegerParameter Name="defaultMaxU16" Size="16" Signed="false" Max="65535"/>
     61     <IntegerParameter Name="defaultMinS32" Size="32" Signed="true" Min="-2147483648"/>
     62     <IntegerParameter Name="defaultMaxS32" Size="32" Signed="true" Max="2147483647"/>
     63     <IntegerParameter Name="defaultMinU32" Size="32" Signed="false" Min="0"/>
     64     <IntegerParameter Name="defaultMaxU32" Size="32" Signed="false" Max="4294967295"/>)"};
     65 const auto &invalidIntegerParameters = Tests<string>{
     66     {"invalid Size(64)", "<IntegerParameter Name='error' Size='64'/>"},
     67     {"minimum > maximum", "<IntegerParameter Name='error' Min='1' Max='0'/>"},
     68     {"S8 minimum > MaxRange", "<IntegerParameter Name='error' Size='8' Signed='true' Min='128'/>"},
     69     {"S8 minimum < MinRange", "<IntegerParameter Name='error' Size='8' Signed='true' Min='-129'/>"},
     70     {"S8 maximum > MaxRange", "<IntegerParameter Name='error' Size='8' Signed='true' Max='128'/>"},
     71     {"S8 maximum < MinRange", "<IntegerParameter Name='error' Size='8' Signed='true' Max='-129'/>"},
     72     {"U8 minimum > MaxRange", "<IntegerParameter Name='error' Size='8' Signed='false' Min='256'/>"},
     73     {"U8 maximum > MaxRange", "<IntegerParameter Name='error' Size='8' Signed='false' Max='256'/>"},
     74     {"S16 minimum > MaxRange",
     75      "<IntegerParameter Name='error' Size='16' Signed='true' Min='32768'/>"},
     76     {"S16 minimum < MinRange",
     77      "<IntegerParameter Name='error' Size='16' Signed='true' Min='-32769'/>"},
     78     {"S16 maximum > MaxRange",
     79      "<IntegerParameter Name='error' Size='16' Signed='true' Max='32768'/>"},
     80     {"S16 maximum < MinRange",
     81      "<IntegerParameter Name='error' Size='16' Signed='true' Max='-32769'/>"},
     82     {"U16 minimum > MaxRange",
     83      "<IntegerParameter Name='error' Size='16' Signed='false' Min='65536'/>"},
     84     {"U16 maximum > MaxRange",
     85      "<IntegerParameter Name='error' Size='16' Signed='false' Max='65536'/>"}};
     86 
     87 struct IntegerPF : public ParameterFramework
     88 {
     89     IntegerPF() : ParameterFramework{std::move(validIntegerInstances)} {}
     90 };
     91 
     92 SCENARIO_METHOD(LazyPF, "Invalid Integer types XML structure", "[Integer types]")
     93 {
     94     for (auto &vec : invalidIntegerParameters) {
     95         GIVEN ("intentional error: " + vec.title) {
     96             create(Config{&Config::instances, vec.payload});
     97             THEN ("Start should fail") {
     98                 CHECK_THROWS_AS(mPf->start(), Exception);
     99             }
    100         }
    101     }
    102 }
    103 
    104 SCENARIO_METHOD(IntegerPF, "Integer types", "[Integer types]")
    105 {
    106     GIVEN ("A valid XML structure file") {
    107         THEN ("Start should succeed") {
    108             CHECK_NOTHROW(start());
    109             REQUIRE_NOTHROW(setTuningMode(true));
    110             string path = "/test/test/nominal";
    111 
    112             AND_THEN ("Set/Get a integer type parameter in real value space") {
    113 
    114                 for (auto &vec : Tests<string>{
    115                          {"(too high)", "13"}, {"(too low)", "-51"}, {"(not a number)", "foobar"},
    116                      }) {
    117                     GIVEN ("Invalid value " + vec.title) {
    118                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
    119                     }
    120                 }
    121                 for (auto &vec : Tests<string>{
    122                          {"(upper limit)", "12"}, {"(lower limit)", "-50"}, {"(inside range)", "0"},
    123                      }) {
    124                     GIVEN ("A valid value " + vec.title) {
    125                         CHECK_NOTHROW(setParameter(path, vec.payload));
    126                         string getValueBack;
    127                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
    128                         CHECK(getValueBack == vec.payload);
    129                     }
    130                 }
    131             }
    132 
    133             AND_THEN ("Set/Get a integer type parameter in raw value space") {
    134                 REQUIRE_NOTHROW(setRawValueSpace(true));
    135                 REQUIRE_NOTHROW(setHexOutputFormat(true));
    136 
    137                 for (auto &vec : Tests<string>{
    138                          {"(too high)", "0x0D"}, {"(too low)", "0xCD"},
    139                      }) {
    140                     GIVEN ("Invalid value " + vec.title) {
    141                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
    142                     }
    143                 }
    144                 for (auto &vec : Tests<string>{
    145                          {"(upper limit)", "0x0C"},
    146                          {"(lower limit)", "0xCE"},
    147                          {"(inside range)", "0x00"},
    148                      }) {
    149                     GIVEN ("A valid value " + vec.title) {
    150                         CHECK_NOTHROW(setParameter(path, vec.payload));
    151                         string getValueBack;
    152                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
    153                         CHECK(getValueBack == vec.payload);
    154                     }
    155                 }
    156             }
    157 
    158             AND_THEN ("Set/Get integer type parameter handle") {
    159                 ElementHandle handle{*this, path};
    160                 /** @FIXME: 'set' operations on a ParameterHandle are silently
    161                  * ignored in tuning mode. Does it make sense ? */
    162                 REQUIRE_NOTHROW(setTuningMode(false));
    163 
    164                 for (auto &vec : Tests<int32_t>{
    165                          {"(upper limit)", 12}, {"(lower limit)", -50}, {"(inside range)", 0},
    166                      }) {
    167                     GIVEN ("A valid value " + vec.title) {
    168                         CHECK_NOTHROW(handle.setAsSignedInteger(vec.payload));
    169                         int32_t getValueBack;
    170                         REQUIRE_NOTHROW(handle.getAsSignedInteger(getValueBack));
    171                         CHECK(getValueBack == vec.payload);
    172                     }
    173                 }
    174                 for (auto &vec : Tests<int32_t>{
    175                          {"(too high)", 13}, {"(too low)", -51},
    176                      }) {
    177                     GIVEN ("An invalid value " + vec.title) {
    178                         CHECK_THROWS_AS(handle.setAsSignedInteger(vec.payload), Exception);
    179                     }
    180                 }
    181             }
    182 
    183             AND_THEN ("Set/Get double type parameter handle") {
    184                 ElementHandle handle{*this, path};
    185                 /** @FIXME: 'set' operations on a ParameterHandle are silently
    186                  * ignored in tuning mode. Does it make sense ? */
    187                 REQUIRE_NOTHROW(setTuningMode(false));
    188 
    189                 for (auto &vec : Tests<double>{
    190                          {"(upper limit)", 12.0f},
    191                          {"(lower limit)", -50.0f},
    192                          {"(inside range)", 0.0f},
    193                      }) {
    194                     GIVEN ("A valid value (rejected not supported)" + vec.title) {
    195                         CHECK_THROWS_AS(handle.setAsDouble(vec.payload), Exception);
    196                     }
    197                 }
    198                 for (auto &vec : Tests<double>{
    199                          {"(too high)", 12.01f}, {"(too low)", -50.01f},
    200                      }) {
    201                     GIVEN ("An invalid value " + vec.title) {
    202                         CHECK_THROWS_AS(handle.setAsDouble(vec.payload), Exception);
    203                     }
    204                 }
    205             }
    206         }
    207     }
    208 }
    209 }
    210