1 /*
2  * Copyright (c) 2017 Nordic Semiconductor ASA
3  * Copyright (c) 2015 Runtime Inc
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include "fcb_test.h"
9 
ZTEST(fcb_test_with_2sectors_set,test_fcb_append_fill)10 ZTEST(fcb_test_with_2sectors_set, test_fcb_append_fill)
11 {
12 	struct fcb *fcb;
13 	int rc;
14 	int i;
15 	struct fcb_entry loc;
16 	uint8_t test_data[128];
17 	int elem_cnts[2] = {0, 0};
18 	int aa_together_cnts[2];
19 	struct append_arg aa_together = {
20 		.elem_cnts = aa_together_cnts
21 	};
22 	int aa_separate_cnts[2];
23 	struct append_arg aa_separate = {
24 		.elem_cnts = aa_separate_cnts
25 	};
26 
27 	fcb = &test_fcb;
28 
29 	for (i = 0; i < sizeof(test_data); i++) {
30 		test_data[i] = fcb_test_append_data(sizeof(test_data), i);
31 	}
32 
33 	while (1) {
34 		rc = fcb_append(fcb, sizeof(test_data), &loc);
35 		if (rc == -ENOSPC) {
36 			break;
37 		}
38 		if (loc.fe_sector == &test_fcb_sector[0]) {
39 			elem_cnts[0]++;
40 		} else if (loc.fe_sector == &test_fcb_sector[1]) {
41 			elem_cnts[1]++;
42 		} else {
43 			zassert_true(0,
44 				     "unexpected flash area of appended loc");
45 		}
46 
47 		rc = flash_area_write(fcb->fap, FCB_ENTRY_FA_DATA_OFF(loc),
48 				      test_data, sizeof(test_data));
49 		zassert_true(rc == 0, "flash_area_write call failure");
50 
51 		rc = fcb_append_finish(fcb, &loc);
52 		zassert_true(rc == 0, "fcb_append_finish call failure");
53 	}
54 	zassert_true(elem_cnts[0] > 0,
55 		     "appendend count should be greater than zero");
56 	zassert_true(elem_cnts[0] == elem_cnts[1],
57 		     "appendend counts should equal to each other");
58 
59 	(void)memset(&aa_together_cnts, 0, sizeof(aa_together_cnts));
60 	rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_together);
61 	zassert_true(rc == 0, "fcb_walk call failure");
62 	zassert_true(aa_together.elem_cnts[0] == elem_cnts[0],
63 		     "fcb_walk: elements count read different than expected");
64 	zassert_true(aa_together.elem_cnts[1] == elem_cnts[1],
65 		     "fcb_walk: elements count read different than expected");
66 
67 	(void)memset(&aa_separate_cnts, 0, sizeof(aa_separate_cnts));
68 	rc = fcb_walk(fcb, &test_fcb_sector[0], fcb_test_cnt_elems_cb,
69 	  &aa_separate);
70 	zassert_true(rc == 0, "fcb_walk call failure");
71 	rc = fcb_walk(fcb, &test_fcb_sector[1], fcb_test_cnt_elems_cb,
72 	  &aa_separate);
73 	zassert_true(rc == 0, "fcb_walk call failure");
74 	zassert_true(aa_separate.elem_cnts[0] == elem_cnts[0],
75 		     "fcb_walk: elements count read different than expected");
76 	zassert_true(aa_separate.elem_cnts[1] == elem_cnts[1],
77 		     "fcb_walk: elements count read different than expected");
78 }
79