|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright (c) 2020 Arduino.  All rights reserved. | 
|  | 3 | + */ | 
|  | 4 | + | 
|  | 5 | +/************************************************************************************** | 
|  | 6 | + * INCLUDE | 
|  | 7 | + **************************************************************************************/ | 
|  | 8 | + | 
|  | 9 | +#include <catch.hpp> | 
|  | 10 | + | 
|  | 11 | +#include <StreamMock.h> | 
|  | 12 | + | 
|  | 13 | +/************************************************************************************** | 
|  | 14 | + * TEST CODE | 
|  | 15 | + **************************************************************************************/ | 
|  | 16 | + | 
|  | 17 | +TEST_CASE ("Testing 'Format' initialisation", "[Stream-insertion-operator-01]") { | 
|  | 18 | +  Format<char> fmt {'a', 2}; | 
|  | 19 | +  REQUIRE(fmt.data == 'a'); | 
|  | 20 | +  REQUIRE(fmt.modifier == 2); | 
|  | 21 | +} | 
|  | 22 | + | 
|  | 23 | +TEST_CASE ("Testing 'format' helper function", "[Stream-insertion-operator-02]") { | 
|  | 24 | +  Format<char> fmt {format('a', 2)}; | 
|  | 25 | +  REQUIRE(fmt.data == 'a'); | 
|  | 26 | +  REQUIRE(fmt.modifier == 2); | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +TEST_CASE ("Testing basic insertion operator", "[Stream-insertion-operator-03]") { | 
|  | 30 | +  StreamMock mock; | 
|  | 31 | +  mock << 'a' << 12 << 'b' << 34;  // Note we cannot test C strings because `StreamMock` has its own << operator. | 
|  | 32 | +  REQUIRE(mock.available() == 6); | 
|  | 33 | + | 
|  | 34 | +  char buf[10] {}; | 
|  | 35 | +  mock.readBytes(buf, 6); | 
|  | 36 | +  REQUIRE(not strncmp(buf, "a12b34", 6)); | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +TEST_CASE ("Testing insertion operator with modifiers", "[Stream-insertion-operator-04]") { | 
|  | 40 | +  StreamMock mock; | 
|  | 41 | +  mock << format(1.2, 4); | 
|  | 42 | +  REQUIRE(mock.available() == 6); | 
|  | 43 | + | 
|  | 44 | +  char buf[10] {}; | 
|  | 45 | +  mock.readBytes(buf, 6); | 
|  | 46 | +  REQUIRE(not strncmp(buf, "1.2000", 6)); | 
|  | 47 | + | 
|  | 48 | +  mock << format(12, BIN); | 
|  | 49 | +  REQUIRE(mock.available() == 4); | 
|  | 50 | + | 
|  | 51 | +  mock.readBytes(buf, 4); | 
|  | 52 | +  REQUIRE(not strncmp(buf, "1100", 4)); | 
|  | 53 | +} | 
0 commit comments