You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/10/17 23:59:38 UTC
[12/16] incubator-mynewt-core git commit: Unit test infrastructure
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/encoding/json/test/src/testcases/json_simple_encode.c
----------------------------------------------------------------------
diff --git a/encoding/json/test/src/testcases/json_simple_encode.c b/encoding/json/test/src/testcases/json_simple_encode.c
new file mode 100644
index 0000000..9e6ceba
--- /dev/null
+++ b/encoding/json/test/src/testcases/json_simple_encode.c
@@ -0,0 +1,85 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "test_json.h"
+
+TEST_CASE(test_json_simple_encode)
+{
+ struct json_encoder encoder;
+ struct json_value value;
+ int rc;
+
+ /* reset the state of the internal test */
+ buf_index = 0;
+ memset(&encoder, 0, sizeof(encoder));
+
+ encoder.je_write = test_write;
+ encoder.je_arg= NULL;
+
+ rc = json_encode_object_start(&encoder);
+ TEST_ASSERT(rc == 0);
+
+ JSON_VALUE_BOOL(&value, 1);
+ rc = json_encode_object_entry(&encoder, "KeyBool", &value);
+ TEST_ASSERT(rc == 0);
+
+ JSON_VALUE_INT(&value, -1234);
+ rc = json_encode_object_entry(&encoder, "KeyInt", &value);
+ TEST_ASSERT(rc == 0);
+
+ JSON_VALUE_UINT(&value, 1353214);
+ rc = json_encode_object_entry(&encoder, "KeyUint", &value);
+ TEST_ASSERT(rc == 0);
+
+ JSON_VALUE_STRING(&value, "foobar");
+ rc = json_encode_object_entry(&encoder, "KeyString", &value);
+ TEST_ASSERT(rc == 0);
+
+ /* we'll decode later differently */
+ JSON_VALUE_STRINGN(&value, "foobarlongstring", 10);
+ rc = json_encode_object_entry(&encoder, "KeyStringN", &value);
+ TEST_ASSERT(rc == 0);
+
+ rc = json_encode_array_name(&encoder, "KeyIntArr");
+ TEST_ASSERT(rc == 0);
+
+ rc = json_encode_array_start(&encoder);
+ TEST_ASSERT(rc == 0);
+
+ JSON_VALUE_INT(&value, 153);
+ rc = json_encode_array_value(&encoder, &value);
+ TEST_ASSERT(rc == 0);
+
+ JSON_VALUE_INT(&value, 2532);
+ rc = json_encode_array_value(&encoder, &value);
+ TEST_ASSERT(rc == 0);
+
+ JSON_VALUE_INT(&value, -322);
+ rc = json_encode_array_value(&encoder, &value);
+ TEST_ASSERT(rc == 0);
+
+ rc = json_encode_array_finish(&encoder);
+ TEST_ASSERT(rc == 0);
+
+ rc = json_encode_object_finish(&encoder);
+ TEST_ASSERT(rc == 0);
+
+ /* does it match what we expect it to */
+ rc = strcmp(bigbuf, output);
+ TEST_ASSERT(rc == 0);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/fcb_test.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/fcb_test.c b/fs/fcb/test/src/fcb_test.c
index 47df15c..e91609e 100644
--- a/fs/fcb/test/src/fcb_test.c
+++ b/fs/fcb/test/src/fcb_test.c
@@ -27,9 +27,11 @@
#include "fcb/fcb.h"
#include "fcb/../../src/fcb_priv.h"
-static struct fcb test_fcb;
+#include "fcb_test.h"
-static struct flash_area test_fcb_area[] = {
+struct fcb test_fcb;
+
+struct flash_area test_fcb_area[] = {
[0] = {
.fa_device_id = 0,
.fa_off = 0,
@@ -52,7 +54,7 @@ static struct flash_area test_fcb_area[] = {
}
};
-static void
+void
fcb_test_wipe(void)
{
int i;
@@ -66,78 +68,20 @@ fcb_test_wipe(void)
}
}
-TEST_CASE(fcb_test_len)
-{
- uint8_t buf[3];
- uint16_t len;
- uint16_t len2;
- int rc;
-
- for (len = 0; len < FCB_MAX_LEN; len++) {
- rc = fcb_put_len(buf, len);
- TEST_ASSERT(rc == 1 || rc == 2);
-
- rc = fcb_get_len(buf, &len2);
- TEST_ASSERT(rc == 1 || rc == 2);
-
- TEST_ASSERT(len == len2);
- }
-}
-
-TEST_CASE(fcb_test_init)
-{
- int rc;
- struct fcb *fcb;
-
- fcb = &test_fcb;
- memset(fcb, 0, sizeof(*fcb));
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == FCB_ERR_ARGS);
-
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == FCB_ERR_ARGS);
-
- fcb->f_sector_cnt = 2;
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-}
-
-static int
+int
fcb_test_empty_walk_cb(struct fcb_entry *loc, void *arg)
{
TEST_ASSERT(0);
return 0;
}
-TEST_CASE(fcb_test_empty_walk)
-{
- int rc;
- struct fcb *fcb;
-
- fcb_test_wipe();
- fcb = &test_fcb;
- memset(fcb, 0, sizeof(*fcb));
-
- fcb->f_sector_cnt = 2;
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-
- rc = fcb_walk(fcb, 0, fcb_test_empty_walk_cb, NULL);
- TEST_ASSERT(rc == 0);
-}
-
-static uint8_t
+uint8_t
fcb_test_append_data(int msg_len, int off)
{
return (msg_len ^ off);
}
-static int
+int
fcb_test_data_walk_cb(struct fcb_entry *loc, void *arg)
{
uint16_t len;
@@ -160,94 +104,7 @@ fcb_test_data_walk_cb(struct fcb_entry *loc, void *arg)
return 0;
}
-TEST_CASE(fcb_test_append)
-{
- int rc;
- struct fcb *fcb;
- struct fcb_entry loc;
- uint8_t test_data[128];
- int i;
- int j;
- int var_cnt;
-
- fcb_test_wipe();
- fcb = &test_fcb;
- memset(fcb, 0, sizeof(*fcb));
- fcb->f_sector_cnt = 2;
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-
- for (i = 0; i < sizeof(test_data); i++) {
- for (j = 0; j < i; j++) {
- test_data[j] = fcb_test_append_data(i, j);
- }
- rc = fcb_append(fcb, i, &loc);
- TEST_ASSERT_FATAL(rc == 0);
- rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, i);
- TEST_ASSERT(rc == 0);
- rc = fcb_append_finish(fcb, &loc);
- TEST_ASSERT(rc == 0);
- }
-
- var_cnt = 0;
- rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(var_cnt == sizeof(test_data));
-}
-
-TEST_CASE(fcb_test_append_too_big)
-{
- struct fcb *fcb;
- int rc;
- int len;
- struct fcb_entry elem_loc;
-
- fcb_test_wipe();
- fcb = &test_fcb;
- memset(fcb, 0, sizeof(*fcb));
- fcb->f_sector_cnt = 2;
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-
- /*
- * Max element which fits inside sector is
- * sector size - (disk header + crc + 1-2 bytes of length).
- */
- len = fcb->f_active.fe_area->fa_size;
-
- rc = fcb_append(fcb, len, &elem_loc);
- TEST_ASSERT(rc != 0);
-
- len--;
- rc = fcb_append(fcb, len, &elem_loc);
- TEST_ASSERT(rc != 0);
-
- len -= sizeof(struct fcb_disk_area);
- rc = fcb_append(fcb, len, &elem_loc);
- TEST_ASSERT(rc != 0);
-
- len = fcb->f_active.fe_area->fa_size -
- (sizeof(struct fcb_disk_area) + 1 + 2);
- rc = fcb_append(fcb, len, &elem_loc);
- TEST_ASSERT(rc == 0);
-
- rc = fcb_append_finish(fcb, &elem_loc);
- TEST_ASSERT(rc == 0);
-
- rc = fcb_elem_info(fcb, &elem_loc);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(elem_loc.fe_data_len == len);
-}
-
-struct append_arg {
- int *elem_cnts;
-};
-
-static int
+int
fcb_test_cnt_elems_cb(struct fcb_entry *loc, void *arg)
{
struct append_arg *aa = (struct append_arg *)arg;
@@ -258,410 +115,34 @@ fcb_test_cnt_elems_cb(struct fcb_entry *loc, void *arg)
return 0;
}
-TEST_CASE(fcb_test_append_fill)
-{
- struct fcb *fcb;
- int rc;
- int i;
- struct fcb_entry loc;
- uint8_t test_data[128];
- int elem_cnts[2] = {0, 0};
- int aa_together_cnts[2];
- struct append_arg aa_together = {
- .elem_cnts = aa_together_cnts
- };
- int aa_separate_cnts[2];
- struct append_arg aa_separate = {
- .elem_cnts = aa_separate_cnts
- };
-
- fcb_test_wipe();
- fcb = &test_fcb;
- memset(fcb, 0, sizeof(*fcb));
- fcb->f_sector_cnt = 2;
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-
- for (i = 0; i < sizeof(test_data); i++) {
- test_data[i] = fcb_test_append_data(sizeof(test_data), i);
- }
-
- while (1) {
- rc = fcb_append(fcb, sizeof(test_data), &loc);
- if (rc == FCB_ERR_NOSPACE) {
- break;
- }
- if (loc.fe_area == &test_fcb_area[0]) {
- elem_cnts[0]++;
- } else if (loc.fe_area == &test_fcb_area[1]) {
- elem_cnts[1]++;
- } else {
- TEST_ASSERT(0);
- }
-
- rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
- sizeof(test_data));
- TEST_ASSERT(rc == 0);
-
- rc = fcb_append_finish(fcb, &loc);
- TEST_ASSERT(rc == 0);
- }
- TEST_ASSERT(elem_cnts[0] > 0);
- TEST_ASSERT(elem_cnts[0] == elem_cnts[1]);
-
- memset(&aa_together_cnts, 0, sizeof(aa_together_cnts));
- rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_together);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(aa_together.elem_cnts[0] == elem_cnts[0]);
- TEST_ASSERT(aa_together.elem_cnts[1] == elem_cnts[1]);
-
- memset(&aa_separate_cnts, 0, sizeof(aa_separate_cnts));
- rc = fcb_walk(fcb, &test_fcb_area[0], fcb_test_cnt_elems_cb,
- &aa_separate);
- TEST_ASSERT(rc == 0);
- rc = fcb_walk(fcb, &test_fcb_area[1], fcb_test_cnt_elems_cb,
- &aa_separate);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(aa_separate.elem_cnts[0] == elem_cnts[0]);
- TEST_ASSERT(aa_separate.elem_cnts[1] == elem_cnts[1]);
-
-}
-
-TEST_CASE(fcb_test_reset)
-{
- struct fcb *fcb;
- int rc;
- int i;
- struct fcb_entry loc;
- uint8_t test_data[128];
- int var_cnt;
-
- fcb_test_wipe();
- fcb = &test_fcb;
- memset(fcb, 0, sizeof(*fcb));
- fcb->f_sector_cnt = 2;
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-
- var_cnt = 0;
- rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(var_cnt == 0);
-
- rc = fcb_append(fcb, 32, &loc);
- TEST_ASSERT(rc == 0);
-
- /*
- * No ready ones yet. CRC should not match.
- */
- var_cnt = 0;
- rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(var_cnt == 0);
-
- for (i = 0; i < sizeof(test_data); i++) {
- test_data[i] = fcb_test_append_data(32, i);
- }
- rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 32);
- TEST_ASSERT(rc == 0);
-
- rc = fcb_append_finish(fcb, &loc);
- TEST_ASSERT(rc == 0);
-
- /*
- * one entry
- */
- var_cnt = 32;
- rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(var_cnt == 33);
-
- /*
- * Pretend reset
- */
- memset(fcb, 0, sizeof(*fcb));
- fcb->f_sector_cnt = 2;
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-
- var_cnt = 32;
- rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(var_cnt == 33);
-
- rc = fcb_append(fcb, 33, &loc);
- TEST_ASSERT(rc == 0);
-
- for (i = 0; i < sizeof(test_data); i++) {
- test_data[i] = fcb_test_append_data(33, i);
- }
- rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 33);
- TEST_ASSERT(rc == 0);
-
- rc = fcb_append_finish(fcb, &loc);
- TEST_ASSERT(rc == 0);
-
- var_cnt = 32;
- rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(var_cnt == 34);
-
- /*
- * Add partial one, make sure that we survive reset then.
- */
- rc = fcb_append(fcb, 34, &loc);
- TEST_ASSERT(rc == 0);
-
- memset(fcb, 0, sizeof(*fcb));
- fcb->f_sector_cnt = 2;
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-
- /*
- * Walk should skip that.
- */
- var_cnt = 32;
- rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(var_cnt == 34);
-
- /* Add a 3rd one, should go behind corrupt entry */
- rc = fcb_append(fcb, 34, &loc);
- TEST_ASSERT(rc == 0);
-
- for (i = 0; i < sizeof(test_data); i++) {
- test_data[i] = fcb_test_append_data(34, i);
- }
- rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 34);
- TEST_ASSERT(rc == 0);
-
- rc = fcb_append_finish(fcb, &loc);
- TEST_ASSERT(rc == 0);
-
- /*
- * Walk should skip corrupt entry, but report the next one.
- */
- var_cnt = 32;
- rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(var_cnt == 35);
-}
-
-TEST_CASE(fcb_test_rotate)
-{
- struct fcb *fcb;
- int rc;
- int old_id;
- struct fcb_entry loc;
- uint8_t test_data[128];
- int elem_cnts[2] = {0, 0};
- int cnts[2];
- struct append_arg aa_arg = {
- .elem_cnts = cnts
- };
-
- fcb_test_wipe();
- fcb = &test_fcb;
- memset(fcb, 0, sizeof(*fcb));
- fcb->f_sector_cnt = 2;
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-
- old_id = fcb->f_active_id;
- rc = fcb_rotate(fcb);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(fcb->f_active_id == old_id + 1);
-
- /*
- * Now fill up the
- */
- while (1) {
- rc = fcb_append(fcb, sizeof(test_data), &loc);
- if (rc == FCB_ERR_NOSPACE) {
- break;
- }
- if (loc.fe_area == &test_fcb_area[0]) {
- elem_cnts[0]++;
- } else if (loc.fe_area == &test_fcb_area[1]) {
- elem_cnts[1]++;
- } else {
- TEST_ASSERT(0);
- }
-
- rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
- sizeof(test_data));
- TEST_ASSERT(rc == 0);
-
- rc = fcb_append_finish(fcb, &loc);
- TEST_ASSERT(rc == 0);
- }
- TEST_ASSERT(elem_cnts[0] > 0 && elem_cnts[0] == elem_cnts[1]);
-
- old_id = fcb->f_active_id;
- rc = fcb_rotate(fcb);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(fcb->f_active_id == old_id); /* no new area created */
-
- memset(cnts, 0, sizeof(cnts));
- rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(aa_arg.elem_cnts[0] == elem_cnts[0] ||
- aa_arg.elem_cnts[1] == elem_cnts[1]);
- TEST_ASSERT(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0);
-
- /*
- * One sector is full. The other one should have one entry in it.
- */
- rc = fcb_append(fcb, sizeof(test_data), &loc);
- TEST_ASSERT(rc == 0);
-
- rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
- sizeof(test_data));
- TEST_ASSERT(rc == 0);
-
- rc = fcb_append_finish(fcb, &loc);
- TEST_ASSERT(rc == 0);
-
- old_id = fcb->f_active_id;
- rc = fcb_rotate(fcb);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(fcb->f_active_id == old_id);
-
- memset(cnts, 0, sizeof(cnts));
- rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
- TEST_ASSERT(rc == 0);
- TEST_ASSERT(aa_arg.elem_cnts[0] == 1 || aa_arg.elem_cnts[1] == 1);
- TEST_ASSERT(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0);
-}
-
-TEST_CASE(fcb_test_multiple_scratch)
-{
- struct fcb *fcb;
- int rc;
- struct fcb_entry loc;
- uint8_t test_data[128];
- int elem_cnts[4];
- int idx;
- int cnts[4];
- struct append_arg aa_arg = {
- .elem_cnts = cnts
- };
-
- fcb_test_wipe();
- fcb = &test_fcb;
- memset(fcb, 0, sizeof(*fcb));
- fcb->f_sector_cnt = 4;
- fcb->f_scratch_cnt = 1;
- fcb->f_sectors = test_fcb_area;
-
- rc = fcb_init(fcb);
- TEST_ASSERT(rc == 0);
-
- /*
- * Now fill up everything. We should be able to get 3 of the sectors
- * full.
- */
- memset(elem_cnts, 0, sizeof(elem_cnts));
- while (1) {
- rc = fcb_append(fcb, sizeof(test_data), &loc);
- if (rc == FCB_ERR_NOSPACE) {
- break;
- }
- idx = loc.fe_area - &test_fcb_area[0];
- elem_cnts[idx]++;
-
- rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
- sizeof(test_data));
- TEST_ASSERT(rc == 0);
-
- rc = fcb_append_finish(fcb, &loc);
- TEST_ASSERT(rc == 0);
- }
-
- TEST_ASSERT(elem_cnts[0] > 0);
- TEST_ASSERT(elem_cnts[0] == elem_cnts[1] && elem_cnts[0] == elem_cnts[2]);
- TEST_ASSERT(elem_cnts[3] == 0);
-
- /*
- * Ask to use scratch block, then fill it up.
- */
- rc = fcb_append_to_scratch(fcb);
- TEST_ASSERT(rc == 0);
-
- while (1) {
- rc = fcb_append(fcb, sizeof(test_data), &loc);
- if (rc == FCB_ERR_NOSPACE) {
- break;
- }
- idx = loc.fe_area - &test_fcb_area[0];
- elem_cnts[idx]++;
-
- rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
- sizeof(test_data));
- TEST_ASSERT(rc == 0);
-
- rc = fcb_append_finish(fcb, &loc);
- TEST_ASSERT(rc == 0);
- }
- TEST_ASSERT(elem_cnts[3] == elem_cnts[0]);
-
- /*
- * Rotate
- */
- rc = fcb_rotate(fcb);
- TEST_ASSERT(rc == 0);
-
- memset(&cnts, 0, sizeof(cnts));
- rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
- TEST_ASSERT(rc == 0);
-
- TEST_ASSERT(cnts[0] == 0);
- TEST_ASSERT(cnts[1] > 0);
- TEST_ASSERT(cnts[1] == cnts[2] && cnts[1] == cnts[3]);
-
- rc = fcb_append_to_scratch(fcb);
- TEST_ASSERT(rc == 0);
- rc = fcb_append_to_scratch(fcb);
- TEST_ASSERT(rc != 0);
-}
+TEST_CASE_DECL(fcb_test_len)
+TEST_CASE_DECL(fcb_test_init)
+TEST_CASE_DECL(fcb_test_empty_walk)
+TEST_CASE_DECL(fcb_test_append)
+TEST_CASE_DECL(fcb_test_append_too_big)
+TEST_CASE_DECL(fcb_test_append_fill)
+TEST_CASE_DECL(fcb_test_reset)
+TEST_CASE_DECL(fcb_test_rotate)
+TEST_CASE_DECL(fcb_test_multiple_scratch)
TEST_SUITE(fcb_test_all)
{
fcb_test_len();
-
fcb_test_init();
-
fcb_test_empty_walk();
-
fcb_test_append();
-
fcb_test_append_too_big();
-
fcb_test_append_fill();
-
fcb_test_reset();
-
fcb_test_rotate();
-
fcb_test_multiple_scratch();
}
#if MYNEWT_VAL(SELFTEST)
-
int
main(int argc, char **argv)
{
- tu_config.tc_print_results = 1;
+ ts_config.ts_print_results = 1;
tu_init();
fcb_test_all();
@@ -669,4 +150,3 @@ main(int argc, char **argv)
return tu_any_failed;
}
#endif
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/fcb_test.h
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/fcb_test.h b/fs/fcb/test/src/fcb_test.h
new file mode 100644
index 0000000..c0ab6c0
--- /dev/null
+++ b/fs/fcb/test/src/fcb_test.h
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef _FCB_TEST_H
+#define _FCB_TEST_H
+
+#include <stdio.h>
+#include <string.h>
+
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "testutil/testutil.h"
+
+#include "fcb/fcb.h"
+#include "fcb/../../src/fcb_priv.h"
+
+#ifdef __cplusplus
+#extern "C" {
+#endif
+
+extern struct fcb test_fcb;
+
+extern struct flash_area test_fcb_area[];
+
+struct append_arg {
+ int *elem_cnts;
+};
+
+void fcb_test_wipe(void);
+int fcb_test_empty_walk_cb(struct fcb_entry *loc, void *arg);
+uint8_t fcb_test_append_data(int msg_len, int off);
+int fcb_test_data_walk_cb(struct fcb_entry *loc, void *arg);
+int fcb_test_cnt_elems_cb(struct fcb_entry *loc, void *arg);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* _FCB_TEST_H */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/testcases/fcb_test_append.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/testcases/fcb_test_append.c b/fs/fcb/test/src/testcases/fcb_test_append.c
new file mode 100644
index 0000000..cba39cd
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_append.c
@@ -0,0 +1,56 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_append)
+{
+ int rc;
+ struct fcb *fcb;
+ struct fcb_entry loc;
+ uint8_t test_data[128];
+ int i;
+ int j;
+ int var_cnt;
+
+ fcb_test_wipe();
+ fcb = &test_fcb;
+ memset(fcb, 0, sizeof(*fcb));
+ fcb->f_sector_cnt = 2;
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+
+ for (i = 0; i < sizeof(test_data); i++) {
+ for (j = 0; j < i; j++) {
+ test_data[j] = fcb_test_append_data(i, j);
+ }
+ rc = fcb_append(fcb, i, &loc);
+ TEST_ASSERT_FATAL(rc == 0);
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, i);
+ TEST_ASSERT(rc == 0);
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+ }
+
+ var_cnt = 0;
+ rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(var_cnt == sizeof(test_data));
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/testcases/fcb_test_append_fill.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/testcases/fcb_test_append_fill.c b/fs/fcb/test/src/testcases/fcb_test_append_fill.c
new file mode 100644
index 0000000..e07f3fc
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_append_fill.c
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_append_fill)
+{
+ struct fcb *fcb;
+ int rc;
+ int i;
+ struct fcb_entry loc;
+ uint8_t test_data[128];
+ int elem_cnts[2] = {0, 0};
+ int aa_together_cnts[2];
+ struct append_arg aa_together = {
+ .elem_cnts = aa_together_cnts
+ };
+ int aa_separate_cnts[2];
+ struct append_arg aa_separate = {
+ .elem_cnts = aa_separate_cnts
+ };
+
+ fcb_test_wipe();
+ fcb = &test_fcb;
+ memset(fcb, 0, sizeof(*fcb));
+ fcb->f_sector_cnt = 2;
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+
+ for (i = 0; i < sizeof(test_data); i++) {
+ test_data[i] = fcb_test_append_data(sizeof(test_data), i);
+ }
+
+ while (1) {
+ rc = fcb_append(fcb, sizeof(test_data), &loc);
+ if (rc == FCB_ERR_NOSPACE) {
+ break;
+ }
+ if (loc.fe_area == &test_fcb_area[0]) {
+ elem_cnts[0]++;
+ } else if (loc.fe_area == &test_fcb_area[1]) {
+ elem_cnts[1]++;
+ } else {
+ TEST_ASSERT(0);
+ }
+
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+ sizeof(test_data));
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+ }
+ TEST_ASSERT(elem_cnts[0] > 0);
+ TEST_ASSERT(elem_cnts[0] == elem_cnts[1]);
+
+ memset(&aa_together_cnts, 0, sizeof(aa_together_cnts));
+ rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_together);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(aa_together.elem_cnts[0] == elem_cnts[0]);
+ TEST_ASSERT(aa_together.elem_cnts[1] == elem_cnts[1]);
+
+ memset(&aa_separate_cnts, 0, sizeof(aa_separate_cnts));
+ rc = fcb_walk(fcb, &test_fcb_area[0], fcb_test_cnt_elems_cb,
+ &aa_separate);
+ TEST_ASSERT(rc == 0);
+ rc = fcb_walk(fcb, &test_fcb_area[1], fcb_test_cnt_elems_cb,
+ &aa_separate);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(aa_separate.elem_cnts[0] == elem_cnts[0]);
+ TEST_ASSERT(aa_separate.elem_cnts[1] == elem_cnts[1]);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/testcases/fcb_test_append_too_big.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/testcases/fcb_test_append_too_big.c b/fs/fcb/test/src/testcases/fcb_test_append_too_big.c
new file mode 100644
index 0000000..0000add
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_append_too_big.c
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_append_too_big)
+{
+ struct fcb *fcb;
+ int rc;
+ int len;
+ struct fcb_entry elem_loc;
+
+ fcb_test_wipe();
+ fcb = &test_fcb;
+ memset(fcb, 0, sizeof(*fcb));
+ fcb->f_sector_cnt = 2;
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+
+ /*
+ * Max element which fits inside sector is
+ * sector size - (disk header + crc + 1-2 bytes of length).
+ */
+ len = fcb->f_active.fe_area->fa_size;
+
+ rc = fcb_append(fcb, len, &elem_loc);
+ TEST_ASSERT(rc != 0);
+
+ len--;
+ rc = fcb_append(fcb, len, &elem_loc);
+ TEST_ASSERT(rc != 0);
+
+ len -= sizeof(struct fcb_disk_area);
+ rc = fcb_append(fcb, len, &elem_loc);
+ TEST_ASSERT(rc != 0);
+
+ len = fcb->f_active.fe_area->fa_size -
+ (sizeof(struct fcb_disk_area) + 1 + 2);
+ rc = fcb_append(fcb, len, &elem_loc);
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &elem_loc);
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_elem_info(fcb, &elem_loc);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(elem_loc.fe_data_len == len);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/testcases/fcb_test_empty_walk.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/testcases/fcb_test_empty_walk.c b/fs/fcb/test/src/testcases/fcb_test_empty_walk.c
new file mode 100644
index 0000000..3a73782
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_empty_walk.c
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_empty_walk)
+{
+ int rc;
+ struct fcb *fcb;
+
+ fcb_test_wipe();
+ fcb = &test_fcb;
+ memset(fcb, 0, sizeof(*fcb));
+
+ fcb->f_sector_cnt = 2;
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_walk(fcb, 0, fcb_test_empty_walk_cb, NULL);
+ TEST_ASSERT(rc == 0);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/testcases/fcb_test_init.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/testcases/fcb_test_init.c b/fs/fcb/test/src/testcases/fcb_test_init.c
new file mode 100644
index 0000000..0e9bdd4
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_init.c
@@ -0,0 +1,40 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_init)
+{
+ int rc;
+ struct fcb *fcb;
+
+ fcb = &test_fcb;
+ memset(fcb, 0, sizeof(*fcb));
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == FCB_ERR_ARGS);
+
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == FCB_ERR_ARGS);
+
+ fcb->f_sector_cnt = 2;
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/testcases/fcb_test_len.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/testcases/fcb_test_len.c b/fs/fcb/test/src/testcases/fcb_test_len.c
new file mode 100644
index 0000000..2ab7065
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_len.c
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_len)
+{
+ uint8_t buf[3];
+ uint16_t len;
+ uint16_t len2;
+ int rc;
+
+ for (len = 0; len < FCB_MAX_LEN; len++) {
+ rc = fcb_put_len(buf, len);
+ TEST_ASSERT(rc == 1 || rc == 2);
+
+ rc = fcb_get_len(buf, &len2);
+ TEST_ASSERT(rc == 1 || rc == 2);
+
+ TEST_ASSERT(len == len2);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/testcases/fcb_test_multiple_scratch.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/testcases/fcb_test_multiple_scratch.c b/fs/fcb/test/src/testcases/fcb_test_multiple_scratch.c
new file mode 100644
index 0000000..b10e7e4
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_multiple_scratch.c
@@ -0,0 +1,110 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_multiple_scratch)
+{
+ struct fcb *fcb;
+ int rc;
+ struct fcb_entry loc;
+ uint8_t test_data[128];
+ int elem_cnts[4];
+ int idx;
+ int cnts[4];
+ struct append_arg aa_arg = {
+ .elem_cnts = cnts
+ };
+
+ fcb_test_wipe();
+ fcb = &test_fcb;
+ memset(fcb, 0, sizeof(*fcb));
+ fcb->f_sector_cnt = 4;
+ fcb->f_scratch_cnt = 1;
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+
+ /*
+ * Now fill up everything. We should be able to get 3 of the sectors
+ * full.
+ */
+ memset(elem_cnts, 0, sizeof(elem_cnts));
+ while (1) {
+ rc = fcb_append(fcb, sizeof(test_data), &loc);
+ if (rc == FCB_ERR_NOSPACE) {
+ break;
+ }
+ idx = loc.fe_area - &test_fcb_area[0];
+ elem_cnts[idx]++;
+
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+ sizeof(test_data));
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+ }
+
+ TEST_ASSERT(elem_cnts[0] > 0);
+ TEST_ASSERT(elem_cnts[0] == elem_cnts[1] && elem_cnts[0] == elem_cnts[2]);
+ TEST_ASSERT(elem_cnts[3] == 0);
+
+ /*
+ * Ask to use scratch block, then fill it up.
+ */
+ rc = fcb_append_to_scratch(fcb);
+ TEST_ASSERT(rc == 0);
+
+ while (1) {
+ rc = fcb_append(fcb, sizeof(test_data), &loc);
+ if (rc == FCB_ERR_NOSPACE) {
+ break;
+ }
+ idx = loc.fe_area - &test_fcb_area[0];
+ elem_cnts[idx]++;
+
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+ sizeof(test_data));
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+ }
+ TEST_ASSERT(elem_cnts[3] == elem_cnts[0]);
+
+ /*
+ * Rotate
+ */
+ rc = fcb_rotate(fcb);
+ TEST_ASSERT(rc == 0);
+
+ memset(&cnts, 0, sizeof(cnts));
+ rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
+ TEST_ASSERT(rc == 0);
+
+ TEST_ASSERT(cnts[0] == 0);
+ TEST_ASSERT(cnts[1] > 0);
+ TEST_ASSERT(cnts[1] == cnts[2] && cnts[1] == cnts[3]);
+
+ rc = fcb_append_to_scratch(fcb);
+ TEST_ASSERT(rc == 0);
+ rc = fcb_append_to_scratch(fcb);
+ TEST_ASSERT(rc != 0);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/testcases/fcb_test_reset.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/testcases/fcb_test_reset.c b/fs/fcb/test/src/testcases/fcb_test_reset.c
new file mode 100644
index 0000000..d54bd1a
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_reset.c
@@ -0,0 +1,145 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_reset)
+{
+ struct fcb *fcb;
+ int rc;
+ int i;
+ struct fcb_entry loc;
+ uint8_t test_data[128];
+ int var_cnt;
+
+ fcb_test_wipe();
+ fcb = &test_fcb;
+ memset(fcb, 0, sizeof(*fcb));
+ fcb->f_sector_cnt = 2;
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+
+ var_cnt = 0;
+ rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(var_cnt == 0);
+
+ rc = fcb_append(fcb, 32, &loc);
+ TEST_ASSERT(rc == 0);
+
+ /*
+ * No ready ones yet. CRC should not match.
+ */
+ var_cnt = 0;
+ rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(var_cnt == 0);
+
+ for (i = 0; i < sizeof(test_data); i++) {
+ test_data[i] = fcb_test_append_data(32, i);
+ }
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 32);
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+
+ /*
+ * one entry
+ */
+ var_cnt = 32;
+ rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(var_cnt == 33);
+
+ /*
+ * Pretend reset
+ */
+ memset(fcb, 0, sizeof(*fcb));
+ fcb->f_sector_cnt = 2;
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+
+ var_cnt = 32;
+ rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(var_cnt == 33);
+
+ rc = fcb_append(fcb, 33, &loc);
+ TEST_ASSERT(rc == 0);
+
+ for (i = 0; i < sizeof(test_data); i++) {
+ test_data[i] = fcb_test_append_data(33, i);
+ }
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 33);
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+
+ var_cnt = 32;
+ rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(var_cnt == 34);
+
+ /*
+ * Add partial one, make sure that we survive reset then.
+ */
+ rc = fcb_append(fcb, 34, &loc);
+ TEST_ASSERT(rc == 0);
+
+ memset(fcb, 0, sizeof(*fcb));
+ fcb->f_sector_cnt = 2;
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+
+ /*
+ * Walk should skip that.
+ */
+ var_cnt = 32;
+ rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(var_cnt == 34);
+
+ /* Add a 3rd one, should go behind corrupt entry */
+ rc = fcb_append(fcb, 34, &loc);
+ TEST_ASSERT(rc == 0);
+
+ for (i = 0; i < sizeof(test_data); i++) {
+ test_data[i] = fcb_test_append_data(34, i);
+ }
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 34);
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+
+ /*
+ * Walk should skip corrupt entry, but report the next one.
+ */
+ var_cnt = 32;
+ rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(var_cnt == 35);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/fcb/test/src/testcases/fcb_test_rotate.c
----------------------------------------------------------------------
diff --git a/fs/fcb/test/src/testcases/fcb_test_rotate.c b/fs/fcb/test/src/testcases/fcb_test_rotate.c
new file mode 100644
index 0000000..398a96a
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_rotate.c
@@ -0,0 +1,108 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_rotate)
+{
+ struct fcb *fcb;
+ int rc;
+ int old_id;
+ struct fcb_entry loc;
+ uint8_t test_data[128];
+ int elem_cnts[2] = {0, 0};
+ int cnts[2];
+ struct append_arg aa_arg = {
+ .elem_cnts = cnts
+ };
+
+ fcb_test_wipe();
+ fcb = &test_fcb;
+ memset(fcb, 0, sizeof(*fcb));
+ fcb->f_sector_cnt = 2;
+ fcb->f_sectors = test_fcb_area;
+
+ rc = fcb_init(fcb);
+ TEST_ASSERT(rc == 0);
+
+ old_id = fcb->f_active_id;
+ rc = fcb_rotate(fcb);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(fcb->f_active_id == old_id + 1);
+
+ /*
+ * Now fill up the
+ */
+ while (1) {
+ rc = fcb_append(fcb, sizeof(test_data), &loc);
+ if (rc == FCB_ERR_NOSPACE) {
+ break;
+ }
+ if (loc.fe_area == &test_fcb_area[0]) {
+ elem_cnts[0]++;
+ } else if (loc.fe_area == &test_fcb_area[1]) {
+ elem_cnts[1]++;
+ } else {
+ TEST_ASSERT(0);
+ }
+
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+ sizeof(test_data));
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+ }
+ TEST_ASSERT(elem_cnts[0] > 0 && elem_cnts[0] == elem_cnts[1]);
+
+ old_id = fcb->f_active_id;
+ rc = fcb_rotate(fcb);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(fcb->f_active_id == old_id); /* no new area created */
+
+ memset(cnts, 0, sizeof(cnts));
+ rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(aa_arg.elem_cnts[0] == elem_cnts[0] ||
+ aa_arg.elem_cnts[1] == elem_cnts[1]);
+ TEST_ASSERT(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0);
+
+ /*
+ * One sector is full. The other one should have one entry in it.
+ */
+ rc = fcb_append(fcb, sizeof(test_data), &loc);
+ TEST_ASSERT(rc == 0);
+
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+ sizeof(test_data));
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+
+ old_id = fcb->f_active_id;
+ rc = fcb_rotate(fcb);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(fcb->f_active_id == old_id);
+
+ memset(cnts, 0, sizeof(cnts));
+ rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(aa_arg.elem_cnts[0] == 1 || aa_arg.elem_cnts[1] == 1);
+ TEST_ASSERT(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/nffs/src/nffs_priv.h
----------------------------------------------------------------------
diff --git a/fs/nffs/src/nffs_priv.h b/fs/nffs/src/nffs_priv.h
index 360194d..0de4420 100644
--- a/fs/nffs/src/nffs_priv.h
+++ b/fs/nffs/src/nffs_priv.h
@@ -511,6 +511,7 @@ int nffs_write_to_file(struct nffs_file *file, const void *data, int len);
#define NFFS_FLASH_LOC_NONE nffs_flash_loc(NFFS_AREA_ID_NONE, 0)
+#if 0
#ifdef ARCH_sim
#include <stdio.h>
#define NFFS_LOG(lvl, ...) \
@@ -519,6 +520,10 @@ int nffs_write_to_file(struct nffs_file *file, const void *data, int len);
#define NFFS_LOG(lvl, ...) \
LOG_ ## lvl(&nffs_log, LOG_MODULE_NFFS, __VA_ARGS__)
#endif
+#endif /* 0 */
+
+#define NFFS_LOG(lvl, ...) \
+ LOG_ ## lvl(&nffs_log, LOG_MODULE_NFFS, __VA_ARGS__)
#ifdef __cplusplus
}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/nffs/syscfg.yml
----------------------------------------------------------------------
diff --git a/fs/nffs/syscfg.yml b/fs/nffs/syscfg.yml
index 657ab0c..98451b0 100644
--- a/fs/nffs/syscfg.yml
+++ b/fs/nffs/syscfg.yml
@@ -7,6 +7,7 @@ syscfg.defs:
value:
restrictions:
- $notnull
+
NFFS_DETECT_FAIL:
description: 'TBD'
value: 'NFFS_DETECT_FAIL_FORMAT'
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/75101ba4/fs/nffs/test/src/arch/cortex_m4/nffs_test.c
----------------------------------------------------------------------
diff --git a/fs/nffs/test/src/arch/cortex_m4/nffs_test.c b/fs/nffs/test/src/arch/cortex_m4/nffs_test.c
deleted file mode 100644
index 654089b..0000000
--- a/fs/nffs/test/src/arch/cortex_m4/nffs_test.c
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#include "nffs/nffs_test.h"
-
-int
-nffs_test_all(void)
-{
- return 0;
-}