You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2015/11/14 02:32:53 UTC

[3/4] incubator-mynewt-larva git commit: Add few tests for flash map/hal_flash..

Add few tests for flash map/hal_flash..


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/12f52004
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/12f52004
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/12f52004

Branch: refs/heads/master
Commit: 12f52004b47df7448897743f388e420011ae4656
Parents: 8554d6a
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Nov 13 17:29:39 2015 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Fri Nov 13 17:29:39 2015 -0800

----------------------------------------------------------------------
 libs/util/src/test/flash_map_test.c | 142 +++++++++++++++++++++++++++++++
 libs/util/src/test/util_test.c      |   3 +-
 libs/util/src/test/util_test_priv.h |   1 +
 3 files changed, 145 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/12f52004/libs/util/src/test/flash_map_test.c
----------------------------------------------------------------------
diff --git a/libs/util/src/test/flash_map_test.c b/libs/util/src/test/flash_map_test.c
new file mode 100644
index 0000000..0d255d5
--- /dev/null
+++ b/libs/util/src/test/flash_map_test.c
@@ -0,0 +1,142 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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 <stdio.h>
+#include <string.h>
+
+#include "os/os.h"
+#include "testutil/testutil.h"
+#include "util/flash_map.h"
+
+#include "hal/hal_flash.h"
+#include "hal/hal_flash_int.h"
+
+/*
+ * Test flash_area_to_sectors()
+ */
+TEST_CASE(flash_map_test_case_1)
+{
+    const struct flash_area *fa;
+    int areas_checked = 0;
+    int i, j, rc;
+    struct hal_flash *hf;
+    struct flash_area my_secs[32];
+    int my_sec_cnt;
+    uint32_t end;
+
+    os_init();
+
+    for (i = 0; i < 8; i++) {
+        rc = flash_area_open(i, &fa);
+        if (rc) {
+            continue;
+        }
+
+        hf = bsp_flash_dev(fa->fa_flash_id);
+        TEST_ASSERT_FATAL(hf != NULL, "bsp_flash_dev");
+
+        rc = flash_area_to_sectors(i, &my_sec_cnt, my_secs);
+        TEST_ASSERT_FATAL(rc == 0, "flash_area_to_sectors failed");
+
+        end = fa->fa_off;
+        for (j = 0; j < my_sec_cnt; j++) {
+            TEST_ASSERT_FATAL(end == my_secs[j].fa_off, "Non contiguous area");
+            TEST_ASSERT_FATAL(my_secs[j].fa_flash_id == fa->fa_flash_id,
+              "Sectors not in same flash?");
+            end = my_secs[j].fa_off + my_secs[j].fa_size;
+        }
+        if (my_sec_cnt) {
+            areas_checked++;
+            TEST_ASSERT_FATAL(my_secs[my_sec_cnt - 1].fa_off +
+              my_secs[my_sec_cnt - 1].fa_size == fa->fa_off + fa->fa_size,
+              "Last sector not in the end");
+        }
+    }
+    TEST_ASSERT_FATAL(areas_checked != 0, "No flash map areas to check!");
+}
+
+/*
+ * Test flash_erase
+ */
+TEST_CASE(flash_map_test_case_2)
+{
+    const struct flash_area *fa;
+    struct flash_area secs[32];
+    int sec_cnt;
+    int i;
+    int rc;
+    uint32_t off;
+    uint8_t wd[256];
+    uint8_t rd[256];
+
+    os_init();
+
+    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fa);
+    TEST_ASSERT_FATAL(rc == 0, "flash_area_open() fail");
+
+    rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &sec_cnt, secs);
+    TEST_ASSERT_FATAL(rc == 0, "flash_area_to_sectors failed");
+
+    memset(wd, 0xa5, sizeof(wd));
+
+    /* write stuff to beginning of every sector */
+    off = 0;
+    for (i = 0; i < sec_cnt; i++) {
+        rc = flash_area_write(fa, off, wd, sizeof(wd));
+        TEST_ASSERT_FATAL(rc == 0, "flash_area_write() fail");
+
+        /* read it back via hal_flash_Read() */
+        rc = hal_flash_read(fa->fa_flash_id, fa->fa_off + off, rd, sizeof(rd));
+        TEST_ASSERT_FATAL(rc == 0, "hal_flash_read() fail");
+
+        rc = memcmp(wd, rd, sizeof(wd));
+        TEST_ASSERT_FATAL(rc == 0, "read data != write data");
+
+        /* write stuff to end of area */
+        rc = hal_flash_write(fa->fa_flash_id,
+          fa->fa_off + off + secs[i].fa_size - sizeof(wd), wd, sizeof(wd));
+        TEST_ASSERT_FATAL(rc == 0, "hal_flash_write() fail");
+
+        /* and read it back */
+        memset(rd, 0, sizeof(rd));
+        rc = flash_area_read(fa, off + secs[i].fa_size - sizeof(rd),
+          rd, sizeof(rd));
+        TEST_ASSERT_FATAL(rc == 0, "hal_flash_read() fail");
+
+        rc = memcmp(wd, rd, sizeof(rd));
+        TEST_ASSERT_FATAL(rc == 0, "read data != write data");
+
+        off += secs[i].fa_size;
+    }
+    /* erase it */
+    rc = flash_area_erase(fa, 0, fa->fa_size);
+    TEST_ASSERT_FATAL(rc == 0, "read data != write data");
+
+    /* should read back ff all throughout*/
+    memset(wd, 0xff, sizeof(wd));
+    for (off = 0; off < fa->fa_size; off += sizeof(rd)) {
+         rc = flash_area_read(fa, off, rd, sizeof(rd));
+         TEST_ASSERT_FATAL(rc == 0, "hal_flash_read() fail");
+
+         rc = memcmp(wd, rd, sizeof(rd));
+         TEST_ASSERT_FATAL(rc == 0, "area not erased");
+    }
+}
+
+TEST_SUITE(flash_map_test_suite)
+{
+    flash_map_test_case_1();
+    flash_map_test_case_2();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/12f52004/libs/util/src/test/util_test.c
----------------------------------------------------------------------
diff --git a/libs/util/src/test/util_test.c b/libs/util/src/test/util_test.c
index 93d4125..9998fa7 100644
--- a/libs/util/src/test/util_test.c
+++ b/libs/util/src/test/util_test.c
@@ -17,12 +17,13 @@
 #include <assert.h>
 #include <stddef.h>
 #include "testutil/testutil.h"
-#include "util_test_priv.h" 
+#include "util_test_priv.h"
 
 int
 util_test_all(void)
 {
     cbmem_test_suite();
+    flash_map_test_suite();
     return tu_case_failed;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/12f52004/libs/util/src/test/util_test_priv.h
----------------------------------------------------------------------
diff --git a/libs/util/src/test/util_test_priv.h b/libs/util/src/test/util_test_priv.h
index 873dded..d6f8da4 100644
--- a/libs/util/src/test/util_test_priv.h
+++ b/libs/util/src/test/util_test_priv.h
@@ -18,5 +18,6 @@
 #define __UTIL_TEST_PRIV_
 
 int cbmem_test_suite(void);
+int flash_map_test_suite(void);
 
 #endif