Skip to content

Commit

Permalink
state(): uCDB state. open(): hash tab pos can't be 0. More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JulStrat committed Jan 28, 2022
1 parent db4522a commit 2081b14
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
90 changes: 90 additions & 0 deletions extras/tests/tests.ino
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,86 @@ bool closed_test3() {
return true;
}

int read_value_without_find_test() {
uCDB<SdFat, File> ucdb(fat);
byte buff[64];

if (ucdb.open("airports.cdb" ) != CDB_OK) {
ucdb.close();
return -1;
}

if (ucdb.state() != CDB_OK)
return -100;

if (ucdb.valueAvailable() != 0)
return -2;
if (ucdb.readValue() != -1)
return -3;
if (ucdb.readValue(buff, 1) != -1)
return -4;

return 1;
}

int read_value_find_fail_test() {
uCDB<SdFat, File> ucdb(fat);
byte buff[64];

if (ucdb.open("airports.cdb" ) != CDB_OK) {
ucdb.close();
return -1;
}

if (ucdb.state() != CDB_OK)
return -100;

if (ucdb.findKey("AAAAAAAA", 8) != KEY_NOT_FOUND)
return -2;

if (ucdb.valueAvailable() != 0)
return -3;
if (ucdb.readValue() != -1)
return -4;
if (ucdb.readValue(buff, 1) != -1)
return -5;

return 1;
}

int read_value_find_ok_test() {
uCDB<SdFat, File> ucdb(fat);
byte buff[64];

if (ucdb.open("airports.cdb" ) != CDB_OK) {
ucdb.close();
return -1;
}

if (ucdb.state() != CDB_OK)
return -100;

if (ucdb.findKey("ZYGH", 4) != KEY_FOUND)
return -2;

if (ucdb.valueAvailable() <= 0)
return -3;

while (ucdb.readValue() >= 0);

if (ucdb.valueAvailable() != 0)
return -4;

if (ucdb.readValue() != -1)
return -5;
if (ucdb.readValue(buff, 1) != 0)
return -6;
if (ucdb.valueAvailable() != 0)
return -7;

return 1;
}

bool random_test1() {
uCDB<SdFat, File> ucdb(fat);
byte buff[64];
Expand Down Expand Up @@ -315,6 +395,16 @@ void loop() {
Serial.println(closed_test1());
Serial.println(closed_test2());
Serial.println(closed_test3());

Serial.println("read_value_without_find_test");
Serial.println(read_value_without_find_test());

Serial.println("read_value_find_fail_test");
Serial.println(read_value_find_fail_test());

Serial.println("read_value_find_ok_test");
Serial.println(read_value_find_ok_test());

Serial.println(random_test1());
Serial.println(random_test2());
Serial.println(random_test3());
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=uCDB
version=0.5.4
version=0.5.5
author=Ioulianos Kakoulidis
maintainer=Ioulianos Kakoulidis <[email protected]>
sentence=API for querying Constant DataBase file store.
Expand Down
15 changes: 10 additions & 5 deletions src/uCDB.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

#define UCDB_VERSION_MAJOR 0
#define UCDB_VERSION_MINOR 5
#define UCDB_VERSION_PATCH 4
#define UCDB_VERSION_PATCH 5

#ifdef TRACE_CDB
#ifndef TracePrinter
Expand Down Expand Up @@ -144,6 +144,13 @@ class uCDB
return (state_ == KEY_FOUND ? valueBytesAvail_ : 0);
}

/**
The UCDB state
*/
cdbResult state() const {
return state_;
}

/**
Close CDB
*/
Expand All @@ -164,7 +171,7 @@ class uCDB
unsigned long keyHash_;

unsigned long dataEndPos_; ///< Data end position
unsigned long slotsNum_; ///< Total slots number in CDB.
unsigned long slotsNum_; ///< Total slots number in CDB

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

if (!htPos) {
continue; // Empty hash table
}
if ((htPos < CDB_HEADER_SIZE) || (htPos > cdb_.size())) {
RETURN(state_ = CDB_ERROR, htPos); // Critical CDB format or data integrity error
}
Expand All @@ -273,6 +277,7 @@ cdbResult uCDB<TFileSystem, TFile>::open(const char *fileName, unsigned long (*u
dataEndPos_ = dend;
slotsNum_ = snum;
hashFunc = userHashFunc;

return (state_ = CDB_OK);
}

Expand Down

0 comments on commit 2081b14

Please sign in to comment.