Unit tests for SmartRest

Dependencies:   C027 SmartRest mbed

Committer:
vwochnik
Date:
Mon Mar 24 10:12:45 2014 +0000
Revision:
0:789029e49ea1
fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vwochnik 0:789029e49ea1 1 #include "FloatValueTest.h"
vwochnik 0:789029e49ea1 2 #include <stdio.h>
vwochnik 0:789029e49ea1 3 #include <assert.h>
vwochnik 0:789029e49ea1 4 #include <string.h>
vwochnik 0:789029e49ea1 5
vwochnik 0:789029e49ea1 6 FloatValueTest::FloatValueTest()
vwochnik 0:789029e49ea1 7 {
vwochnik 0:789029e49ea1 8 }
vwochnik 0:789029e49ea1 9
vwochnik 0:789029e49ea1 10 void FloatValueTest::test()
vwochnik 0:789029e49ea1 11 {
vwochnik 0:789029e49ea1 12 testValue(3, 0, "3");
vwochnik 0:789029e49ea1 13 testValue(3, 1, "3.0");
vwochnik 0:789029e49ea1 14 testValue(3, 5, "3.00000");
vwochnik 0:789029e49ea1 15 testValue(3.123456789, 0, "3");
vwochnik 0:789029e49ea1 16 testValue(3.123456789, 1, "3.1");
vwochnik 0:789029e49ea1 17 testValue(3.123456789, 5, "3.12346");
vwochnik 0:789029e49ea1 18 testValue(3.513456789, 1, "3.5");
vwochnik 0:789029e49ea1 19 testValue(3.593456789, 1, "3.6");
vwochnik 0:789029e49ea1 20 }
vwochnik 0:789029e49ea1 21
vwochnik 0:789029e49ea1 22 void FloatValueTest::testValue(double number, uint8_t digits, const char *expected)
vwochnik 0:789029e49ea1 23 {
vwochnik 0:789029e49ea1 24 printf("Expecting output '%s' for number %f with precision %d\n", expected, number, digits);
vwochnik 0:789029e49ea1 25 FloatValue value(number, digits);
vwochnik 0:789029e49ea1 26 assert(value.length() == strlen(expected));
vwochnik 0:789029e49ea1 27 assert(value.valueType() == VALUE_FLOAT);
vwochnik 0:789029e49ea1 28 assert(value.floatValue() == number);
vwochnik 0:789029e49ea1 29 value.write(sink);
vwochnik 0:789029e49ea1 30 assert(strcmp(expected, sink.value()) == 0);
vwochnik 0:789029e49ea1 31 sink.clear();
vwochnik 0:789029e49ea1 32
vwochnik 0:789029e49ea1 33 Value* copy = value.copy();
vwochnik 0:789029e49ea1 34 assert(copy->floatValue() == value.floatValue());
vwochnik 0:789029e49ea1 35 assert(copy->length() == value.length());
vwochnik 0:789029e49ea1 36 assert(copy->valueType() == value.valueType());
vwochnik 0:789029e49ea1 37 delete copy;
vwochnik 0:789029e49ea1 38 }
vwochnik 0:789029e49ea1 39