Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / tests / eeprom.cpp
blobaa92eaebd2a7d90c858a0448224dc03c07d66540
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)
38 rambackupWrite();
39 Backup::RamBackupUncompressed ramBackupRestored;
40 if (uncompress((uint8_t *)&ramBackupRestored, sizeof(ramBackupRestored), ramBackup->data, ramBackup->size) != sizeof(ramBackupUncompressed))
41 TRACE("ERROR uncompress");
42 if (memcmp(&ramBackupUncompressed, &ramBackupRestored, sizeof(ramBackupUncompressed)) != 0)
43 TRACE("ERROR restore");
45 #endif
47 #if defined(EEPROM_RLC)
48 TEST(Eeprom, 100_random_writes)
50 eepromFile = NULL; // in memory
51 RlcFile f;
52 uint8_t buf[1000];
53 uint8_t buf2[1000];
55 storageFormat();
57 for (int i = 0; i < 100; i++) {
58 int size = rand() % 800;
59 for (int j = 0; j < size; j++) {
60 buf[j] = rand() < (RAND_MAX / 10000 * i) ? 0 : (j & 0xff);
62 f.writeRlc(5, 5, buf, size, 100);
63 // printf("size=%4d red=%4d\n\n\n", size, f.size());
64 f.openRd(5);
65 uint16_t n = f.readRlc(buf2,size+1);
66 EXPECT_EQ(n, size);
67 EXPECT_EQ(memcmp(buf, buf2, size), 0);
71 TEST(Eeprom, test2)
73 eepromFile = NULL; // in memory
74 RlcFile f;
75 uint8_t buf[1000];
77 storageFormat();
79 for (int i = 0; i < 1000; i++)
80 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) {
90 EXPECT_EQ(b, ('6' + sz % 4));
92 sz+=n;
94 EXPECT_EQ(sz, 300);
97 TEST(Eeprom, storageCheckImmediately)
99 eepromFile = NULL; // in memory
100 // RlcFile f;
101 uint8_t buf[1000];
103 storageFormat();
105 for (int i = 0; i < 1000; i++)
106 buf[i]='6'+i%4;
108 theFile.writeRlc(6, 6, buf, 300, false);
110 storageCheck(true);
112 theFile.openRd(6);
113 uint16_t sz=0;
114 for (int i = 0; i < 500; i++) {
115 uint8_t b;
116 uint16_t n = theFile.readRlc(&b, 1);
117 if (n) {
118 EXPECT_EQ(b, ('6' + sz % 4));
120 sz += n;
122 EXPECT_EQ(sz, 300);
125 TEST(Eeprom, copy)
127 eepromFile = NULL; // in memory
129 uint8_t buf[1000];
131 storageFormat();
133 for (int i = 0; i < 1000; i++)
134 buf[i] = '6' + i % 4;
136 theFile.writeRlc(5, 6, buf, 300, true);
138 theFile.copy(6, 5);
140 theFile.openRd(6);
141 uint16_t sz = 0;
142 for (int i = 0; i < 500; i++) {
143 uint8_t b;
144 uint16_t n = theFile.readRlc(&b, 1);
145 if (n) {
146 EXPECT_EQ(b, ('6' + sz % 4));
148 sz += n;
150 EXPECT_EQ(sz, 300);
153 TEST(Eeprom, rm)
155 eepromFile = NULL; // in memory
157 uint8_t buf[1000];
159 storageFormat();
161 for (int i = 0; i < 1000; i++)
162 buf[i] = '6' + i % 4;
164 theFile.writeRlc(5, 6, buf, 300, true);
166 EXPECT_EQ(EFile::exists(5), true);
168 EFile::rm(5);
170 EXPECT_EQ(EFile::exists(5), false);
172 theFile.openRd(5);
173 uint16_t sz=0;
174 for(int i=0; i<500; i++){
175 uint8_t b;
176 uint16_t n = theFile.readRlc(&b, 1);
177 if (n) {
178 EXPECT_EQ(b, ('6' + sz % 4));
180 sz += n;
182 EXPECT_EQ(sz, 0);
184 #endif