changeset 205:84de8ebabd62

bulbs are properly tested now
author Louis Opter <kalessin@kalessin.fr>
date Sat, 25 Jul 2015 21:14:49 -0700
parents e3b49c843612
children 18000309f5f7
files tag_untag_testing.patch
diffstat 1 files changed, 158 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/tag_untag_testing.patch	Fri Jul 24 01:08:35 2015 -0700
+++ b/tag_untag_testing.patch	Sat Jul 25 21:14:49 2015 -0700
@@ -1,5 +1,5 @@
 # HG changeset patch
-# Parent  e6ce6a7734c436a200cf02c491fceff3420e98de
+# Parent  b82dfd3266817224388aad97e687b852b4ae658f
 
 diff --git a/CMakeLists.txt b/CMakeLists.txt
 --- a/CMakeLists.txt
@@ -873,19 +873,102 @@
 +ENDFOREACH()
 diff --git a/tests/lifx/bulb/test_bulb_close.c b/tests/lifx/bulb/test_bulb_close.c
 new file mode 100644
+--- /dev/null
++++ b/tests/lifx/bulb/test_bulb_close.c
+@@ -0,0 +1,33 @@
++#include "bulb.c"
++
++#include "mock_gateway.h"
++
++int
++main(void)
++{
++    struct lgtd_lifx_gateway gw;
++    uint8_t bulb_addr[LGTD_LIFX_ADDR_LENGTH] = { 5, 4, 3, 2, 1, 0 };
++    struct lgtd_lifx_bulb *bulb = lgtd_lifx_bulb_open(&gw, bulb_addr);
++
++    bulb->state.power = LGTD_LIFX_POWER_ON;
++    LGTD_STATS_ADD_AND_UPDATE_PROCTITLE(bulbs_powered_on, 1);
++
++    lgtd_lifx_bulb_close(bulb);
++
++    if (!RB_EMPTY(&lgtd_lifx_bulbs_table)) {
++        errx(1, "The bulbs table should be empty!");
++    }
++
++    if (LGTD_STATS_GET(bulbs) != 0) {
++        errx(1, "The bulbs counter is %d (expected 0)", LGTD_STATS_GET(bulbs));
++    }
++
++    if (LGTD_STATS_GET(bulbs_powered_on) != 0) {
++        errx(
++            1, "The powered on bulbs counter is %d (expected 0)",
++            LGTD_STATS_GET(bulbs_powered_on)
++        );
++    }
++
++    return 0;
++}
 diff --git a/tests/lifx/bulb/test_bulb_open.c b/tests/lifx/bulb/test_bulb_open.c
 new file mode 100644
+--- /dev/null
++++ b/tests/lifx/bulb/test_bulb_open.c
+@@ -0,0 +1,44 @@
++#include "bulb.c"
++
++#include "mock_gateway.h"
++
++int
++main(void)
++{
++    struct lgtd_lifx_gateway gw;
++    uint8_t bulb_addr[LGTD_LIFX_ADDR_LENGTH] = { 5, 4, 3, 2, 1, 0 };
++    lgtd_time_mono_t now = lgtd_time_monotonic_msecs();
++    struct lgtd_lifx_bulb *bulb = lgtd_lifx_bulb_open(&gw, bulb_addr);
++
++    if (!bulb) {
++        errx(1, "lgtd_lifx_bulb_open didn't return any bulb");
++    }
++
++    if (memcmp(bulb->addr, bulb_addr, LGTD_LIFX_ADDR_LENGTH)) {
++        errx(
++            1, "got bulb addr %s (expected %s)",
++            lgtd_addrtoa(bulb->addr), lgtd_addrtoa(bulb_addr)
++        );
++    }
++
++    if (bulb->gw != &gw) {
++        errx(1, "got bulb gateway %p (expected %p)", bulb->gw, &gw);
++    }
++
++    if (lgtd_lifx_bulb_get(bulb_addr) != bulb) {
++        errx(1, "the new bulb can't be found");
++    }
++
++    if (bulb->last_light_state_at < now) {
++        errx(
++            1, "got bulb->last_light_state_at %ju (expected >= %ju)",
++            bulb->last_light_state_at, (uintmax_t)now
++        );
++    }
++
++    if (LGTD_STATS_GET(bulbs) != 1) {
++        errx(1, "bulbs counter is %d (expected 1)", LGTD_STATS_GET(bulbs));
++    }
++
++    return 0;
++}
 diff --git a/tests/lifx/bulb/test_bulb_set_light_state.c b/tests/lifx/bulb/test_bulb_set_light_state.c
 new file mode 100644
 --- /dev/null
 +++ b/tests/lifx/bulb/test_bulb_set_light_state.c
-@@ -0,0 +1,73 @@
+@@ -0,0 +1,92 @@
 +#include "bulb.c"
 +
 +#define MOCKED_LGTD_LIFX_GATEWAY_UPDATE_TAG_REFCOUNTS
 +#include "mock_gateway.h"
 +
-+static bool update_tag_refcouts_called = false;
++static int update_tag_refcouts_call_counts = 0;
 +
 +void
 +lgtd_lifx_gateway_update_tag_refcounts(struct lgtd_lifx_gateway *gw,
@@ -896,15 +979,21 @@
 +        errx(1, "got wrong gw %p (expected 0xdeaf)", gw);
 +    }
 +
-+    if (bulb_tags != 0x2a) {
-+        errx(1, "got bulb_tags %#jx (expected 0x2a)", (uintmax_t)bulb_tags);
-+    }
-+
 +    if (pkt_tags != 0xfeed) {
 +        errx(1, "got pkt_tags %#jx (expected 0xfeed)", (uintmax_t)pkt_tags);
 +    }
 +
-+    update_tag_refcouts_called = true;
++    if (!update_tag_refcouts_call_counts) {
++        if (bulb_tags != 0x2a) {
++            errx(1, "got bulb_tags %#jx (expected 0x2a)", (uintmax_t)bulb_tags);
++        }
++    } else {
++        if (bulb_tags != 0xfeed) {
++            errx(1, "got bulb_tags %#jx (expected 0xfeed)", (uintmax_t)bulb_tags);
++        }
++    }
++
++    update_tag_refcouts_call_counts++;
 +}
 +
 +int
@@ -916,7 +1005,7 @@
 +            .brightness = UINT16_MAX,
 +            .kelvin = 12345,
 +            .dim = 808,
-+            .power = 0,
++            .power = LGTD_LIFX_POWER_OFF,
 +            .label = "lair",
 +            .tags = 0x2a
 +        },
@@ -928,33 +1017,88 @@
 +        .brightness = UINT16_MAX / 2,
 +        .kelvin = 54321,
 +        .dim = 303,
-+        .power = 0,
++        .power = LGTD_LIFX_POWER_ON,
 +        .label = "caverne",
 +        .tags = 0xfeed
 +    };
 +
 +    lgtd_lifx_bulb_set_light_state(&bulb, &new_state, 2015);
-+
 +    if (memcmp(&bulb.state, &new_state, sizeof(new_state))) {
 +        errx(1, "new light state incorrectly set");
 +    }
-+
++    if (LGTD_STATS_GET(bulbs_powered_on) != 1) {
++        errx(
++            1, "unexpected bulbs_powered_on counter value %d (expected 1)",
++            LGTD_STATS_GET(bulbs_powered_on)
++        );
++    }
 +    if (bulb.last_light_state_at != 2015) {
 +        errx(
 +            1, "got bulb.last_light_state = %jx (expected 2015)",
 +            (uintmax_t)bulb.last_light_state_at
 +        );
 +    }
++    if (update_tag_refcouts_call_counts != 1) {
++        errx(1, "lgtd_lifx_gateway_update_tag_refcounts wasn't called");
++    }
 +
-+    if (!update_tag_refcouts_called) {
++    lgtd_lifx_bulb_set_light_state(&bulb, &new_state, 2015);
++    if (update_tag_refcouts_call_counts != 2) {
 +        errx(1, "lgtd_lifx_gateway_update_tag_refcounts wasn't called");
 +    }
++    if (LGTD_STATS_GET(bulbs_powered_on) != 1) {
++        errx(
++            1, "unexpected bulbs_powered_on counter value %d (expected 1)",
++            LGTD_STATS_GET(bulbs_powered_on)
++        );
++    }
 +
 +    return 0;
 +}
-+
 diff --git a/tests/lifx/bulb/test_bulb_set_power_state.c b/tests/lifx/bulb/test_bulb_set_power_state.c
 new file mode 100644
+--- /dev/null
++++ b/tests/lifx/bulb/test_bulb_set_power_state.c
+@@ -0,0 +1,39 @@
++#include "bulb.c"
++
++#include "mock_gateway.h"
++
++int
++main(void)
++{
++    struct lgtd_lifx_bulb bulb = {
++        .state = {
++            .hue = 54321,
++            .brightness = UINT16_MAX,
++            .kelvin = 12345,
++            .dim = 808,
++            .power = LGTD_LIFX_POWER_OFF,
++            .label = "lair",
++            .tags = 0x2a
++        },
++        .gw = (void *)0xdeaf
++    };
++    struct lgtd_lifx_light_state new_state;
++    memcpy(&new_state, &bulb.state, sizeof(new_state));
++    new_state.power = LGTD_LIFX_POWER_ON;
++
++
++    for (int i = 0; i != 2; i++) {
++        lgtd_lifx_bulb_set_power_state(&bulb, LGTD_LIFX_POWER_ON);
++        if (memcmp(&bulb.state, &new_state, sizeof(new_state))) {
++            errx(1, "new light state incorrectly set");
++        }
++        if (LGTD_STATS_GET(bulbs_powered_on) != 1) {
++            errx(
++                1, "unexpected bulbs_powered_on counter value %d (expected 1)",
++                LGTD_STATS_GET(bulbs_powered_on)
++            );
++        }
++    }
++
++    return 0;
++}
 diff --git a/tests/lifx/bulb/test_bulb_set_tags.c b/tests/lifx/bulb/test_bulb_set_tags.c
 new file mode 100644
 --- /dev/null