1 /*
2  * Copyright (c) 2023, Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/drivers/retained_mem.h>
7 #include <zephyr/ztest.h>
8 #include <errno.h>
9 
10 const static struct device *retained_mem_test_device =	DEVICE_DT_GET(DT_ALIAS(
11 									retainedmemtestdevice));
12 
13 #if CONFIG_RETAINED_MEM_SIZE_LIMITED
14 /* For size-limited tests, use data size of 1 byte */
15 static uint8_t data[1] = {
16 	0x5b,
17 };
18 static uint8_t empty_data[1] = {
19 	0x00,
20 };
21 static uint8_t buffer[1];
22 #else
23 /* For other devices, use data size of 10 bytes */
24 static uint8_t data[10] = {
25 	0x23, 0x82, 0xa8, 0x7b, 0xde, 0x18, 0x00, 0xff, 0x8e, 0xd6,
26 };
27 static uint8_t empty_data[10] = {
28 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29 };
30 static uint8_t buffer[10];
31 #endif
32 
ZTEST(retained_mem_api,test_read_write)33 ZTEST(retained_mem_api, test_read_write)
34 {
35 	int32_t rc;
36 
37 	rc = retained_mem_write(retained_mem_test_device, 0, data, sizeof(data));
38 	zassert_equal(rc, 0, "Return code should be success");
39 
40 	memset(buffer, 0, sizeof(buffer));
41 
42 	rc = retained_mem_read(retained_mem_test_device, 0, buffer, sizeof(buffer));
43 	zassert_equal(rc, 0, "Return code should be success");
44 
45 	zassert_mem_equal(data, buffer, sizeof(data), "Expected written data to match");
46 }
47 
ZTEST(retained_mem_api,test_size)48 ZTEST(retained_mem_api, test_size)
49 {
50 	int32_t rc;
51 
52 	rc = retained_mem_size(retained_mem_test_device);
53 	zassume_between_inclusive(rc, 1, 0x4000, "Retained memory size is not valid");
54 }
55 
ZTEST(retained_mem_api,test_clear)56 ZTEST(retained_mem_api, test_clear)
57 {
58 	int32_t rc;
59 
60 	rc = retained_mem_clear(retained_mem_test_device);
61 	zassert_equal(rc, 0, "Return code should be success");
62 
63 	rc = retained_mem_write(retained_mem_test_device, 0, data, sizeof(data));
64 	zassert_equal(rc, 0, "Return code should be success");
65 
66 	rc = retained_mem_read(retained_mem_test_device, 0, buffer, sizeof(buffer));
67 	zassert_equal(rc, 0, "Return code should be success");
68 
69 	zassert_mem_equal(data, buffer, sizeof(data), "Expected written data to match");
70 
71 	rc = retained_mem_clear(retained_mem_test_device);
72 	zassert_equal(rc, 0, "Return code should be success");
73 
74 	rc = retained_mem_read(retained_mem_test_device, 0, buffer, sizeof(buffer));
75 	zassert_equal(rc, 0, "Return code should be success");
76 
77 	zassert_mem_equal(empty_data, buffer, sizeof(empty_data), "Expected data to be 0x00's");
78 }
79 
80 ZTEST_SUITE(retained_mem_api, NULL, NULL, NULL, NULL, NULL);
81