|
| 1 | +/* |
| 2 | + This file is part of the ArduinoBLE library. |
| 3 | + Copyright (c) 2018 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <catch2/catch_test_macros.hpp> |
| 21 | + |
| 22 | +#define private public |
| 23 | +#define protected public |
| 24 | + |
| 25 | +#include "FakeBLELocalDevice.h" |
| 26 | +#include "BLEAdvertisingData.h" |
| 27 | +#include "BLETypedCharacteristics.h" |
| 28 | +#include "BLELocalCharacteristic.h" |
| 29 | +#include "BLEStringCharacteristic.h" |
| 30 | +#include "BLEProperty.h" |
| 31 | + |
| 32 | + |
| 33 | +TEST_CASE("Test characteristic writeValue", "[ArduinoBLE::BLECharacteristic]") |
| 34 | +{ |
| 35 | + WHEN("Create a bool characteristic") |
| 36 | + { |
| 37 | + BLEBoolCharacteristic boolCharacteristic("Bool", BLERead | BLEIndicate | BLEWrite); |
| 38 | + bool v = false;; |
| 39 | + int written = boolCharacteristic.writeValue(v); |
| 40 | + REQUIRE( written == sizeof(bool) ); |
| 41 | + } |
| 42 | + |
| 43 | + WHEN("Create a boolean characteristic") |
| 44 | + { |
| 45 | + BLEBooleanCharacteristic booleanCharacteristic("Boolean", BLERead | BLEIndicate | BLEWrite); |
| 46 | + bool v = false; |
| 47 | + int written = booleanCharacteristic.writeValue(v); |
| 48 | + REQUIRE( written == sizeof(bool) ); |
| 49 | + } |
| 50 | + |
| 51 | + WHEN("Create a char characteristic") |
| 52 | + { |
| 53 | + BLECharCharacteristic charCharacteristic("Char", BLERead | BLEIndicate | BLEWrite); |
| 54 | + char v = 'a'; |
| 55 | + int written = charCharacteristic.writeValue(v); |
| 56 | + REQUIRE( written == sizeof(char) ); |
| 57 | + } |
| 58 | + |
| 59 | + WHEN("Create a unsigned char characteristic") |
| 60 | + { |
| 61 | + BLEUnsignedCharCharacteristic unsignedCharCharacteristic("UnsignedChar", BLERead | BLEIndicate | BLEWrite); |
| 62 | + unsigned char v = 0x01; |
| 63 | + int written = unsignedCharCharacteristic.writeValue(v); |
| 64 | + REQUIRE( written == sizeof(unsigned char) ); |
| 65 | + } |
| 66 | + |
| 67 | + WHEN("Create a byte characteristic") |
| 68 | + { |
| 69 | + BLEByteCharacteristic byteCharacteristic("Byte", BLERead | BLEIndicate | BLEWrite); |
| 70 | + byte v = 0x01; |
| 71 | + int written = byteCharacteristic.writeValue(v); |
| 72 | + REQUIRE( written == sizeof(byte) ); |
| 73 | + } |
| 74 | + |
| 75 | + WHEN("Create a short characteristic") |
| 76 | + { |
| 77 | + BLEShortCharacteristic shortCharacteristic("Short", BLERead | BLEIndicate | BLEWrite); |
| 78 | + short v = -1; |
| 79 | + int written = shortCharacteristic.writeValue(v); |
| 80 | + REQUIRE( written == sizeof(short) ); |
| 81 | + } |
| 82 | + |
| 83 | + WHEN("Create a unsigned short characteristic") |
| 84 | + { |
| 85 | + BLEUnsignedShortCharacteristic unsignedShortCharacteristic("UnsignedShort", BLERead | BLEIndicate | BLEWrite); |
| 86 | + unsigned short v = 1; |
| 87 | + int written = unsignedShortCharacteristic.writeValue(v); |
| 88 | + REQUIRE( written == sizeof(unsigned short) ); |
| 89 | + } |
| 90 | + |
| 91 | + WHEN("Create a word characteristic") |
| 92 | + { |
| 93 | + BLEWordCharacteristic wordCharacteristic("Word", BLERead | BLEIndicate | BLEWrite); |
| 94 | + word v = -1; |
| 95 | + int written = wordCharacteristic.writeValue(v); |
| 96 | + REQUIRE( written == sizeof(word) ); |
| 97 | + } |
| 98 | + |
| 99 | + WHEN("Create a int characteristic") |
| 100 | + { |
| 101 | + BLEIntCharacteristic intCharacteristic("Int", BLERead | BLEIndicate | BLEWrite); |
| 102 | + int v = -1; |
| 103 | + int written = intCharacteristic.writeValue(v); |
| 104 | + REQUIRE( written == sizeof(int) ); |
| 105 | + } |
| 106 | + |
| 107 | + WHEN("Create a unsigned int characteristic") |
| 108 | + { |
| 109 | + BLEUnsignedIntCharacteristic unsignedIntCharacteristic("UnsignedInt", BLERead | BLEIndicate | BLEWrite); |
| 110 | + unsigned int v = 1; |
| 111 | + int written = unsignedIntCharacteristic.writeValue(v); |
| 112 | + REQUIRE( written == sizeof(unsigned int) ); |
| 113 | + } |
| 114 | + |
| 115 | + WHEN("Create a long characteristic") |
| 116 | + { |
| 117 | + BLELongCharacteristic longCharacteristic("Long", BLERead | BLEIndicate | BLEWrite); |
| 118 | + long v = -1; |
| 119 | + int written = longCharacteristic.writeValue(v); |
| 120 | + REQUIRE( written == sizeof(long) ); |
| 121 | + } |
| 122 | + |
| 123 | + WHEN("Create a unsigned long characteristic") |
| 124 | + { |
| 125 | + BLEUnsignedLongCharacteristic unsignedLongCharacteristic("UnsignedLong", BLERead | BLEIndicate | BLEWrite); |
| 126 | + unsigned long v = 1; |
| 127 | + int written = unsignedLongCharacteristic.writeValue(v); |
| 128 | + REQUIRE( written == sizeof(unsigned long) ); |
| 129 | + } |
| 130 | + |
| 131 | + WHEN("Create a float characteristic") |
| 132 | + { |
| 133 | + BLEFloatCharacteristic floatCharacteristic("Float", BLERead | BLEIndicate | BLEWrite); |
| 134 | + float v = -1.0f; |
| 135 | + int written = floatCharacteristic.writeValue(v); |
| 136 | + REQUIRE( written == sizeof(float) ); |
| 137 | + } |
| 138 | + |
| 139 | + WHEN("Create a double characteristic") |
| 140 | + { |
| 141 | + BLEDoubleCharacteristic doubleCharacteristic("Double", BLERead | BLEIndicate | BLEWrite); |
| 142 | + double v = -1.0; |
| 143 | + int written = doubleCharacteristic.writeValue(v); |
| 144 | + REQUIRE( written == sizeof(double) ); |
| 145 | + } |
| 146 | + |
| 147 | + WHEN("Create a string characteristic") |
| 148 | + { |
| 149 | + const int maxStringLength = 64; |
| 150 | + BLEStringCharacteristic stringCharacteristic("String", BLERead | BLEIndicate | BLEWrite, maxStringLength); |
| 151 | + const char* v = "Hello"; |
| 152 | + int written = stringCharacteristic.writeValue(v); |
| 153 | + REQUIRE( written == min(strlen(v), maxStringLength) ); |
| 154 | + } |
| 155 | + |
| 156 | + WHEN("Create a too long string characteristic") |
| 157 | + { |
| 158 | + const int maxStringLength = 4; |
| 159 | + BLEStringCharacteristic stringCharacteristic("String", BLERead | BLEIndicate | BLEWrite, maxStringLength); |
| 160 | + const char* v = "Hello"; |
| 161 | + int written = stringCharacteristic.writeValue(v); |
| 162 | + REQUIRE( written == min(strlen(v), maxStringLength) ); |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments