|
| 1 | +/* |
| 2 | + Copyright 2020 Leon Kiefer |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | +#line 17 "UnitTests.ino" |
| 17 | + |
| 18 | +#include <AUnit.h> |
| 19 | + |
| 20 | +#include "FastLEDControllerUtils.h" |
| 21 | + |
| 22 | +using namespace aunit; |
| 23 | + |
| 24 | +class FastLEDControllerTest : public TestOnce { |
| 25 | +protected: |
| 26 | + void assertCRGB(const CRGB& actual, const CRGB& expected) { |
| 27 | + assertEqual(actual.r, expected.r); |
| 28 | + assertEqual(actual.g, expected.g); |
| 29 | + assertEqual(actual.b, expected.b); |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +test(getLEDs) { |
| 34 | + CRGB leds[10]; |
| 35 | + FastLEDController ledController(false); |
| 36 | + ledController.addLEDs(0, leds, 10); |
| 37 | + assertEqual(ledController.getLEDs(0), leds); |
| 38 | + assertEqual(ledController.getLEDs(1), nullptr); |
| 39 | +} |
| 40 | + |
| 41 | +testF(FastLEDControllerTest, simpleScaleUp) { |
| 42 | + CRGB leds[20]; |
| 43 | + FastLEDController ledController(false); |
| 44 | + fill_solid(leds, 20, CRGB::Black); |
| 45 | + ledController.addLEDs(0, leds, 10); |
| 46 | + |
| 47 | + fill_solid(leds, 10, CRGB::White); |
| 48 | + CLP::scale(&ledController, 0, 20); |
| 49 | + |
| 50 | + for (int i = 0; i < 10; i++) { |
| 51 | + assertCRGB(leds[i], CRGB::White); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +testF(FastLEDControllerTest, simpleScaleDown) { |
| 56 | + CRGB leds[20]; |
| 57 | + FastLEDController ledController(false); |
| 58 | + fill_solid(leds, 20, CRGB::Black); |
| 59 | + ledController.addLEDs(0, leds, 20); |
| 60 | + |
| 61 | + fill_solid(leds, 10, CRGB::White); |
| 62 | + CLP::scale(&ledController, 0, 10); |
| 63 | + |
| 64 | + for (int i = 0; i < 5; i++) { |
| 65 | + assertCRGB(leds[i], CRGB::White); |
| 66 | + } |
| 67 | + for (int i = 5; i < 10; i++) { |
| 68 | + assertCRGB(leds[i], CRGB::Black); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +testF(FastLEDControllerTest, simpleScaleIdentity) { |
| 73 | + CRGB leds[20]; |
| 74 | + FastLEDController ledController(false); |
| 75 | + fill_solid(leds, 20, CRGB::Black); |
| 76 | + ledController.addLEDs(0, leds, 10); |
| 77 | + |
| 78 | + fill_solid(leds, 10, CRGB::White); |
| 79 | + CLP::scale(&ledController, 0, 10); |
| 80 | + |
| 81 | + for (int i = 0; i < 10; i++) { |
| 82 | + assertCRGB(leds[i], CRGB::White); |
| 83 | + } |
| 84 | + for (int i = 10; i < 20; i++) { |
| 85 | + assertCRGB(leds[i], CRGB::Black); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +testF(FastLEDControllerTest, LT100) { |
| 90 | + CRGB leds[30]; |
| 91 | + FastLEDController ledController(false); |
| 92 | + fill_solid(leds, 30, CRGB::Black); |
| 93 | + ledController.addLEDs(0, leds, 30); |
| 94 | + |
| 95 | + leds[0] = CRGB::White; |
| 96 | + fill_solid(leds + 1, 26, CRGB::Blue); |
| 97 | + CLP::SegmentScaling segments[2] = {{1, 4}, {26, 26}}; |
| 98 | + CLP::scaleSegments(&ledController, 0, segments, 2); |
| 99 | + |
| 100 | + for (int i = 0; i < 4; i++) { |
| 101 | + assertCRGB(leds[i], CRGB::White); |
| 102 | + } |
| 103 | + for (int i = 4; i < 30; i++) { |
| 104 | + assertCRGB(leds[i], CRGB::Blue); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +testF(FastLEDControllerTest, singleSegmentScaleUp) { |
| 109 | + CRGB leds[20]; |
| 110 | + FastLEDController ledController(false); |
| 111 | + fill_solid(leds, 20, CRGB::Black); |
| 112 | + ledController.addLEDs(0, leds, 20); |
| 113 | + |
| 114 | + fill_solid(leds, 10, CRGB::White); |
| 115 | + CLP::SegmentScaling segments[] = {{10, 20}}; |
| 116 | + CLP::scaleSegments(&ledController, 0, segments, 1); |
| 117 | + |
| 118 | + for (int i = 0; i < 20; i++) { |
| 119 | + assertCRGB(leds[i], CRGB::White); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +testF(FastLEDControllerTest, multiScaleUp) { |
| 124 | + CRGB leds[30]; |
| 125 | + FastLEDController ledController(false); |
| 126 | + fill_solid(leds, 30, CRGB::Black); |
| 127 | + ledController.addLEDs(0, leds, 10); |
| 128 | + |
| 129 | + fill_solid(leds + 5, 5, CRGB::White); |
| 130 | + CLP::SegmentScaling segments[] = {{5, 10}, {5, 20}}; |
| 131 | + CLP::scaleSegments(&ledController, 0, segments, 2); |
| 132 | + |
| 133 | + for (int i = 0; i < 10; i++) { |
| 134 | + assertCRGB(leds[i], CRGB::Black); |
| 135 | + } |
| 136 | + for (int i = 10; i < 30; i++) { |
| 137 | + assertCRGB(leds[i], CRGB::White); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +testF(FastLEDControllerTest, multiScaleDown) { |
| 142 | + CRGB leds[30]; |
| 143 | + FastLEDController ledController(false); |
| 144 | + fill_solid(leds, 30, CRGB::Black); |
| 145 | + ledController.addLEDs(0, leds, 30); |
| 146 | + |
| 147 | + fill_solid(leds + 10, 20, CRGB::White); |
| 148 | + CLP::SegmentScaling segments[] = {{10, 5}, {20, 5}}; |
| 149 | + CLP::scaleSegments(&ledController, 0, segments, 2); |
| 150 | + |
| 151 | + for (int i = 0; i < 5; i++) { |
| 152 | + assertCRGB(leds[i], CRGB::Black); |
| 153 | + } |
| 154 | + for (int i = 5; i < 10; i++) { |
| 155 | + assertCRGB(leds[i], CRGB::White); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +testF(FastLEDControllerTest, singleSegmentScaleDown) { |
| 160 | + CRGB leds[20]; |
| 161 | + FastLEDController ledController(false); |
| 162 | + fill_solid(leds, 20, CRGB::Black); |
| 163 | + ledController.addLEDs(0, leds, 20); |
| 164 | + |
| 165 | + fill_solid(leds, 10, CRGB::White); |
| 166 | + CLP::SegmentScaling segments[] = {{20, 10}}; |
| 167 | + CLP::scaleSegments(&ledController, 0, segments, 1); |
| 168 | + |
| 169 | + for (int i = 0; i < 5; i++) { |
| 170 | + assertCRGB(leds[i], CRGB::White); |
| 171 | + } |
| 172 | + for (int i = 5; i < 10; i++) { |
| 173 | + assertCRGB(leds[i], CRGB::Black); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +testF(FastLEDControllerTest, SegmentScaleOverlap) { |
| 178 | + CRGB leds[15]; |
| 179 | + FastLEDController ledController(false); |
| 180 | + fill_solid(leds, 15, CRGB::Black); |
| 181 | + ledController.addLEDs(0, leds, 15); |
| 182 | + |
| 183 | + fill_solid(leds, 5, CRGB::White); |
| 184 | + CLP::SegmentScaling segments[] = {{5, 10}, {10, 5}}; |
| 185 | + CLP::scaleSegments(&ledController, 0, segments, 2); |
| 186 | + |
| 187 | + for (int i = 0; i < 10; i++) { |
| 188 | + assertCRGB(leds[i], CRGB::White); |
| 189 | + } |
| 190 | + for (int i = 10; i < 15; i++) { |
| 191 | + assertCRGB(leds[i], CRGB::Black); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +testF(FastLEDControllerTest, SegmentScaleOverlapInverted) { |
| 196 | + CRGB leds[15]; |
| 197 | + FastLEDController ledController(false); |
| 198 | + fill_solid(leds, 15, CRGB::Black); |
| 199 | + ledController.addLEDs(0, leds, 15); |
| 200 | + |
| 201 | + fill_solid(leds, 10, CRGB::White); |
| 202 | + CLP::SegmentScaling segments[] = {{10, 5}, {5, 10}}; |
| 203 | + CLP::scaleSegments(&ledController, 0, segments, 2); |
| 204 | + |
| 205 | + for (int i = 0; i < 5; i++) { |
| 206 | + assertCRGB(leds[i], CRGB::White); |
| 207 | + } |
| 208 | + for (int i = 5; i < 15; i++) { |
| 209 | + assertCRGB(leds[i], CRGB::Black); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +testF(FastLEDControllerTest, SegmentScaleMix) { |
| 214 | + CRGB leds[30]; |
| 215 | + FastLEDController ledController(false); |
| 216 | + fill_solid(leds, 30, CRGB::Black); |
| 217 | + ledController.addLEDs(0, leds, 30); |
| 218 | + |
| 219 | + fill_solid(leds, 5, CRGB::White); |
| 220 | + fill_solid(leds + 5, 20, CRGB::Red); |
| 221 | + fill_solid(leds + 25, 5, CRGB::Blue); |
| 222 | + CLP::SegmentScaling segments[] = {{5, 10}, {20, 5}, {5, 10}}; |
| 223 | + CLP::scaleSegments(&ledController, 0, segments, 3); |
| 224 | + |
| 225 | + for (int i = 0; i < 10; i++) { |
| 226 | + assertCRGB(leds[i], CRGB::White); |
| 227 | + } |
| 228 | + for (int i = 10; i < 15; i++) { |
| 229 | + assertCRGB(leds[i], CRGB::Red); |
| 230 | + } |
| 231 | + for (int i = 15; i < 25; i++) { |
| 232 | + assertCRGB(leds[i], CRGB::Blue); |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +testF(FastLEDControllerTest, SegmentScaleMixInverted) { |
| 237 | + CRGB leds[30]; |
| 238 | + FastLEDController ledController(false); |
| 239 | + fill_solid(leds, 30, CRGB::Black); |
| 240 | + ledController.addLEDs(0, leds, 25); |
| 241 | + |
| 242 | + fill_solid(leds, 10, CRGB::White); |
| 243 | + fill_solid(leds + 10, 5, CRGB::Red); |
| 244 | + fill_solid(leds + 15, 10, CRGB::Blue); |
| 245 | + CLP::SegmentScaling segments[] = {{10, 5}, {5, 20}, {10, 5}}; |
| 246 | + CLP::scaleSegments(&ledController, 0, segments, 3); |
| 247 | + |
| 248 | + for (int i = 0; i < 5; i++) { |
| 249 | + assertCRGB(leds[i], CRGB::White); |
| 250 | + } |
| 251 | + for (int i = 5; i < 25; i++) { |
| 252 | + assertCRGB(leds[i], CRGB::Red); |
| 253 | + } |
| 254 | + for (int i = 25; i < 30; i++) { |
| 255 | + assertCRGB(leds[i], CRGB::Blue); |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +void setup() { |
| 260 | + delay(1000); |
| 261 | + Serial.begin(115200); |
| 262 | + while (!Serial) |
| 263 | + ; |
| 264 | +} |
| 265 | + |
| 266 | +void loop() { |
| 267 | + TestRunner::run(); |
| 268 | +} |
0 commit comments