Fix doc path
[opentx.git] / radio / src / tests / eeprom.cpp
blob7dce423c7e8d2170c01a598cef29bc0c3e9eb7c2
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "gtests.h"
23 extern const char * eepromFile;
25 #if !defined(EEPROM) && defined(SDCARD)
26 namespace Backup {
27 #define BACKUP
28 #include "datastructs.h"
29 PACK(struct RamBackupUncompressed {
30 ModelData model;
31 RadioData radio;
32 });
33 #undef BACKUP
35 extern Backup::RamBackupUncompressed ramBackupUncompressed;
36 TEST(Storage, BackupAndRestore)
39 rambackupWrite();
40 Backup::RamBackupUncompressed ramBackupRestored;
41 if (uncompress((uint8_t *)&ramBackupRestored, sizeof(ramBackupRestored), ramBackup->data, ramBackup->size) != sizeof(ramBackupUncompressed))
42 TRACE("ERROR uncompress");
43 if (memcmp(&ramBackupUncompressed, &ramBackupRestored, sizeof(ramBackupUncompressed)) != 0)
44 TRACE("ERROR restore");
46 #endif
48 #if defined(EEPROM_RLC)
49 TEST(Eeprom, 100_random_writes)
51 eepromFile = NULL; // in memory
52 RlcFile f;
53 uint8_t buf[1000];
54 uint8_t buf2[1000];
56 storageFormat();
58 for(int i=0; i<100; i++) {
59 int size = rand()%800;
60 for(int j=0; j<size; j++) {
61 buf[j] = rand() < (RAND_MAX/10000*i) ? 0 : (j&0xff);
63 f.writeRlc(5, 5, buf, size, 100);
64 // printf("size=%4d red=%4d\n\n\n", size, f.size());
65 f.openRd(5);
66 uint16_t n = f.readRlc(buf2,size+1);
67 EXPECT_EQ(n, size);
68 EXPECT_EQ(memcmp(buf, buf2, size), 0);
72 TEST(Eeprom, test2)
74 eepromFile = NULL; // in memory
75 RlcFile f;
76 uint8_t buf[1000];
78 storageFormat();
80 for(int i=0; i<1000; i++) buf[i]='6'+i%4;
82 f.writeRlc(6, 6, buf, 300, 100);
84 f.openRd(6);
85 uint16_t sz=0;
86 for(int i=0; i<500; i++){
87 uint8_t b;
88 uint16_t n=f.readRlc(&b,1);
89 if(n) EXPECT_EQ(b, ('6'+sz%4));
90 sz+=n;
92 EXPECT_EQ(sz, 300);
95 TEST(Eeprom, storageCheckImmediately)
97 eepromFile = NULL; // in memory
98 // RlcFile f;
99 uint8_t buf[1000];
101 storageFormat();
103 for(int i=0; i<1000; i++) buf[i]='6'+i%4;
105 theFile.writeRlc(6, 6, buf, 300, false);
107 storageCheck(true);
109 theFile.openRd(6);
110 uint16_t sz=0;
111 for(int i=0; i<500; i++){
112 uint8_t b;
113 uint16_t n=theFile.readRlc(&b,1);
114 if(n) EXPECT_EQ(b, ('6'+sz%4));
115 sz+=n;
117 EXPECT_EQ(sz, 300);
120 TEST(Eeprom, copy)
122 eepromFile = NULL; // in memory
124 uint8_t buf[1000];
126 storageFormat();
128 for(int i=0; i<1000; i++) buf[i]='6'+i%4;
130 theFile.writeRlc(5, 6, buf, 300, true);
132 theFile.copy(6, 5);
134 theFile.openRd(6);
135 uint16_t sz=0;
136 for(int i=0; i<500; i++){
137 uint8_t b;
138 uint16_t n=theFile.readRlc(&b,1);
139 if(n) EXPECT_EQ(b, ('6'+sz%4));
140 sz+=n;
142 EXPECT_EQ(sz, 300);
145 TEST(Eeprom, rm)
147 eepromFile = NULL; // in memory
149 uint8_t buf[1000];
151 storageFormat();
153 for(int i=0; i<1000; i++) buf[i]='6'+i%4;
155 theFile.writeRlc(5, 6, buf, 300, true);
157 EXPECT_EQ(EFile::exists(5), true);
159 EFile::rm(5);
161 EXPECT_EQ(EFile::exists(5), false);
163 theFile.openRd(5);
164 uint16_t sz=0;
165 for(int i=0; i<500; i++){
166 uint8_t b;
167 uint16_t n=theFile.readRlc(&b,1);
168 if(n) EXPECT_EQ(b, ('6'+sz%4));
169 sz+=n;
171 EXPECT_EQ(sz, 0);
173 #endif