25 std::map<std::string, std::string>::iterator i = mappings.find(name);
26 if (i != mappings.end()) {
28 return msg->
getInt(i->second.c_str(), ok);
30 return msg->
getAsInt(i->second.c_str());
45 std::map<std::string, std::string>::iterator i = mappings.find(name);
46 if (i != mappings.end()) {
48 return msg->
getFloat(i->second.c_str(), ok);
65 std::map<std::string, std::string>::iterator i = mappings.find(name);
66 if (i != mappings.end()) {
68 return msg->
getTime(i->second.c_str(), ok);
85 std::map<std::string, std::string>::iterator i = mappings.find(name);
86 if (i != mappings.end()) {
88 return msg->
getString(i->second.c_str(), ok);
111 unittest::fail(
"Observations test: default ctor cid != OBSERVATIONID\n");
115 unittest::fail(
"Observations test: ObsLocation type != OBSID_LOC\n");
119 unittest::fail(
"Observations test: header.size != sizeof(ObsHeader)\n");
125 unittest::fail(
"Observations test: ObsInteger type != OBSID_INT\n");
130 unittest::fail(
"Observations test: ObsFloat type != OBSID_FLOAT\n");
134 unittest::fail(
"Observations test: GetObsTypeFromRawData(NULL) != 0\n");
150 msg.
setInt(
"ObjectID", 4242);
154 const uint64 created = 1234567890ULL;
157 std::map<std::string, std::string> mappings;
161 unittest::fail(
"Observations test: extractDataFromMessage failed\n");
164 if (loc.
data.x < 1.49 || loc.
data.x > 1.51 ||
165 loc.
data.y < 2.49 || loc.
data.y > 2.51 ||
166 loc.
data.z < 3.49 || loc.
data.z > 3.51) {
167 unittest::fail(
"Observations test: extracted location x/y/z mismatch\n");
170 if (loc.
data.mapID != 7) {
175 unittest::fail(
"Observations test: extracted objectID mismatch\n");
179 unittest::fail(
"Observations test: extracted creatorID (systemID) mismatch\n");
183 unittest::fail(
"Observations test: extracted timestamp mismatch\n");
187 unittest::fail(
"Observations test: extracted confidence mismatch\n");
195 unittest::fail(
"Observations test: extract from empty message should fail\n");
205 msg.
setInt(
"reading", -55);
206 std::map<std::string, std::string> mappings;
207 mappings[
"val"] =
"reading";
214 if (obs.
data.val != -55) {
231 unittest::fail(
"Observations test: in-range value reported outside\n");
235 unittest::fail(
"Observations test: out-of-range value reported inside\n");
246 obs.
data.val = 42.25;
248 const uint64
id = 1001;
255 unittest::fail(
"Observations test: duplicate writeToMessage should fail\n");
264 if (back.
data.val < 42.24 || back.
data.val > 42.26) {
269 unittest::fail(
"Observations test: round-trip objectID mismatch\n");
275 unittest::fail(
"Observations test: readFromMessage of missing id should fail\n");
286 for (uint32 k = 1; k <= n; k++) {
288 obs.
data.val = (int64)(k * 10);
290 unittest::fail(
"Observations test: writeToMessage in loop failed\n");
294 std::list<GenericObservation<ObsInteger>*> all =
296 if (all.size() != n) {
297 unittest::fail(
"Observations test: ReadAllFromMessage count mismatch\n");
298 std::list<GenericObservation<ObsInteger>*>::iterator ci = all.begin();
299 while (ci != all.end()) {
delete *ci; ci++; }
303 std::list<GenericObservation<ObsInteger>*>::iterator i = all.begin(), e = all.end();
305 sum += (*i)->data.val;
307 if ((*i)->print().empty() || (*i)->toJSON().empty()) {
309 std::list<GenericObservation<ObsInteger>*>::iterator ci = all.begin();
310 while (ci != all.end()) {
delete *ci; ci++; }
316 if (sum != 10 + 20 + 30 + 40 + 50) {
317 unittest::fail(
"Observations test: ReadAllFromMessage value sum mismatch\n");
329 "Observation construct/extract/range/serialise round-trip",
"data");
Typed "observation" records extracted from DataMessages: location, dimension, integer,...
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
Small, dependency-free unit test harness used by all CMSDK object tests.
The central Psyclone data container: a self-contained binary message with typed, named user entries.
bool setFloat(const char *key, float64 value)
setFloat(const char* key, float64 value)
bool getInt(const char *key, int64 &value)
getInt(const char* key, int64& value)
bool setInt(const char *key, int64 value)
setInt(const char* key, int64 value)
std::string getAsString(const char *key)
getAsString(const char* key)
bool setCreatedTime(uint64 t)
setCreatedTime(uint64 t)
uint64 getTime(const char *key)
getTime(const char* key)
bool setSystemID(uint32 id)
setSystemID(uint32 id) Set message system id
bool getFloat(const char *key, float64 &value)
getFloat(const char* key, float64& value)
int64 getAsInt(const char *key)
getAsInt(const char* key)
const char * getString(const char *key)
getString(const char* key)
uint32 getContentType(const char *key)
getContentType(const char* key)
float64 getAsFloat(const char *key)
getAsFloat(const char* key)
Header + typed payload wrapper for one observation, serializable to/from DataMessage and raw memory.
bool extractDataFromMessage(DataMessage *msg, std::map< std::string, std::string > &mappings)
Populate the payload from a message using logical→actual field mappings.
ObsHeader header
Identity/provenance/confidence header.
bool readFromMessage(DataMessage *msg, uint64 id=0)
Read the observation with id id (0 = first) from msg.
static std::list< GenericObservation< T > * > ReadAllFromMessage(DataMessage *msg)
Deserialize every observation of type T carried by msg.
bool writeToMessage(DataMessage *msg, uint64 id)
Append this observation to msg under entry id id.
bool isBetween(GenericObservation< T > *a, GenericObservation< T > *b)
Range test delegated to the payload's isBetween(); see per-type notes on semantics.
static UnitTestRunner & instance()
Access the singleton (created on first use).
void registerTest(const char *name, UnitTestFunc func, const char *description="", const char *category="", bool inDefaultRun=true)
Register a test with the runner.
void fail(const char *fmt,...)
Set an explanatory reason shown on the FAIL line.
void metric(const char *name, double value, const char *unit="", bool higherIsBetter=true)
Record a performance metric.
void progress(int percent, const char *action)
Report progress with a short description of the current action.
std::string ExtractStringFromMessage(const char *name, DataMessage *msg, std::map< std::string, std::string > &mappings, bool &ok)
String variant of ExtractIntFromMessage(); same mapping semantics.
uint64 ExtractTimeFromMessage(const char *name, DataMessage *msg, std::map< std::string, std::string > &mappings, bool &ok)
Timestamp variant of ExtractIntFromMessage(); same mapping semantics.
float64 ExtractFloatFromMessage(const char *name, DataMessage *msg, std::map< std::string, std::string > &mappings, bool &ok)
Float variant of ExtractIntFromMessage(); same mapping semantics.
bool Observations_UnitTest()
Unit test for the Observations module.
uint32 GetObsTypeFromRawData(const char *data, uint32 size)
Inspect a raw observation blob and return its OBSID_* type tag from the embedded ObsHeader.
int64 ExtractIntFromMessage(const char *name, DataMessage *msg, std::map< std::string, std::string > &mappings, bool &ok)
Read an integer field from a message, translating name through mappings first.
static struct PsyType CTRL_TEST
void Register_Observations_Tests()
Register the Observations tests with the unit test framework.