Skip to content

Commit 2081b14

Browse files
committed
state(): uCDB state. open(): hash tab pos can't be 0. More tests
1 parent db4522a commit 2081b14

File tree

3 files changed

+101
-6
lines changed

3 files changed

+101
-6
lines changed

extras/tests/tests.ino

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,86 @@ bool closed_test3() {
133133
return true;
134134
}
135135

136+
int read_value_without_find_test() {
137+
uCDB<SdFat, File> ucdb(fat);
138+
byte buff[64];
139+
140+
if (ucdb.open("airports.cdb" ) != CDB_OK) {
141+
ucdb.close();
142+
return -1;
143+
}
144+
145+
if (ucdb.state() != CDB_OK)
146+
return -100;
147+
148+
if (ucdb.valueAvailable() != 0)
149+
return -2;
150+
if (ucdb.readValue() != -1)
151+
return -3;
152+
if (ucdb.readValue(buff, 1) != -1)
153+
return -4;
154+
155+
return 1;
156+
}
157+
158+
int read_value_find_fail_test() {
159+
uCDB<SdFat, File> ucdb(fat);
160+
byte buff[64];
161+
162+
if (ucdb.open("airports.cdb" ) != CDB_OK) {
163+
ucdb.close();
164+
return -1;
165+
}
166+
167+
if (ucdb.state() != CDB_OK)
168+
return -100;
169+
170+
if (ucdb.findKey("AAAAAAAA", 8) != KEY_NOT_FOUND)
171+
return -2;
172+
173+
if (ucdb.valueAvailable() != 0)
174+
return -3;
175+
if (ucdb.readValue() != -1)
176+
return -4;
177+
if (ucdb.readValue(buff, 1) != -1)
178+
return -5;
179+
180+
return 1;
181+
}
182+
183+
int read_value_find_ok_test() {
184+
uCDB<SdFat, File> ucdb(fat);
185+
byte buff[64];
186+
187+
if (ucdb.open("airports.cdb" ) != CDB_OK) {
188+
ucdb.close();
189+
return -1;
190+
}
191+
192+
if (ucdb.state() != CDB_OK)
193+
return -100;
194+
195+
if (ucdb.findKey("ZYGH", 4) != KEY_FOUND)
196+
return -2;
197+
198+
if (ucdb.valueAvailable() <= 0)
199+
return -3;
200+
201+
while (ucdb.readValue() >= 0);
202+
203+
if (ucdb.valueAvailable() != 0)
204+
return -4;
205+
206+
if (ucdb.readValue() != -1)
207+
return -5;
208+
if (ucdb.readValue(buff, 1) != 0)
209+
return -6;
210+
if (ucdb.valueAvailable() != 0)
211+
return -7;
212+
213+
return 1;
214+
}
215+
136216
bool random_test1() {
137217
uCDB<SdFat, File> ucdb(fat);
138218
byte buff[64];
@@ -315,6 +395,16 @@ void loop() {
315395
Serial.println(closed_test1());
316396
Serial.println(closed_test2());
317397
Serial.println(closed_test3());
398+
399+
Serial.println("read_value_without_find_test");
400+
Serial.println(read_value_without_find_test());
401+
402+
Serial.println("read_value_find_fail_test");
403+
Serial.println(read_value_find_fail_test());
404+
405+
Serial.println("read_value_find_ok_test");
406+
Serial.println(read_value_find_ok_test());
407+
318408
Serial.println(random_test1());
319409
Serial.println(random_test2());
320410
Serial.println(random_test3());

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=uCDB
2-
version=0.5.4
2+
version=0.5.5
33
author=Ioulianos Kakoulidis
44
maintainer=Ioulianos Kakoulidis <ioulianos.kakoulidis@hotmail.com>
55
sentence=API for querying Constant DataBase file store.

src/uCDB.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
#define UCDB_VERSION_MAJOR 0
5252
#define UCDB_VERSION_MINOR 5
53-
#define UCDB_VERSION_PATCH 4
53+
#define UCDB_VERSION_PATCH 5
5454

5555
#ifdef TRACE_CDB
5656
#ifndef TracePrinter
@@ -144,6 +144,13 @@ class uCDB
144144
return (state_ == KEY_FOUND ? valueBytesAvail_ : 0);
145145
}
146146

147+
/**
148+
The UCDB state
149+
*/
150+
cdbResult state() const {
151+
return state_;
152+
}
153+
147154
/**
148155
Close CDB
149156
*/
@@ -164,7 +171,7 @@ class uCDB
164171
unsigned long keyHash_;
165172

166173
unsigned long dataEndPos_; ///< Data end position
167-
unsigned long slotsNum_; ///< Total slots number in CDB.
174+
unsigned long slotsNum_; ///< Total slots number in CDB
168175

169176
unsigned int hashTabID_; ///< Last accessed hash table
170177
/// @name Hash table descriptor (HEADER section)
@@ -245,9 +252,6 @@ cdbResult uCDB<TFileSystem, TFile>::open(const char *fileName, unsigned long (*u
245252
htPos = unpack(buff);
246253
htSlotsNum = unpack(buff + 4);
247254

248-
if (!htPos) {
249-
continue; // Empty hash table
250-
}
251255
if ((htPos < CDB_HEADER_SIZE) || (htPos > cdb_.size())) {
252256
RETURN(state_ = CDB_ERROR, htPos); // Critical CDB format or data integrity error
253257
}
@@ -273,6 +277,7 @@ cdbResult uCDB<TFileSystem, TFile>::open(const char *fileName, unsigned long (*u
273277
dataEndPos_ = dend;
274278
slotsNum_ = snum;
275279
hashFunc = userHashFunc;
280+
276281
return (state_ = CDB_OK);
277282
}
278283

0 commit comments

Comments
 (0)