Unit tests for SmartRest

Dependencies:   C027 SmartRest mbed

Revision:
0:789029e49ea1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/values/FloatValueTest.cpp	Mon Mar 24 10:12:45 2014 +0000
@@ -0,0 +1,39 @@
+#include "FloatValueTest.h"
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+
+FloatValueTest::FloatValueTest()
+{
+}
+
+void FloatValueTest::test()
+{
+    testValue(3, 0, "3");
+    testValue(3, 1, "3.0");
+    testValue(3, 5, "3.00000");
+    testValue(3.123456789, 0, "3");
+    testValue(3.123456789, 1, "3.1");
+    testValue(3.123456789, 5, "3.12346");
+    testValue(3.513456789, 1, "3.5");
+    testValue(3.593456789, 1, "3.6");
+}
+
+void FloatValueTest::testValue(double number, uint8_t digits, const char *expected)
+{
+    printf("Expecting output '%s' for number %f with precision %d\n", expected, number, digits);
+    FloatValue value(number, digits);
+    assert(value.length() == strlen(expected));
+    assert(value.valueType() == VALUE_FLOAT);
+    assert(value.floatValue() == number);
+    value.write(sink);
+    assert(strcmp(expected, sink.value()) == 0);
+    sink.clear();
+
+    Value* copy = value.copy();
+    assert(copy->floatValue() == value.floatValue());
+    assert(copy->length() == value.length());
+    assert(copy->valueType() == value.valueType());
+    delete copy;
+}
+