automerge/rust/automerge-c/test/cmocka_utils.h
Jason Kankiewicz 8de2fa9bd4
C API 2 (#530)
The AMvalue union, AMlistItem struct, AMmapItem struct, and AMobjItem struct are gone, replaced by the AMitem struct.

The AMchangeHashes, AMchanges, AMlistItems, AMmapItems, AMobjItems, AMstrs, and AMsyncHaves iterators are gone, replaced by the AMitems iterator.

The AMitem struct is opaque, getting and setting values is now achieved exclusively through function calls.

The AMitemsNext(), AMitemsPrev(), and AMresultItem() functions return a pointer to an AMitem struct so you ultimately get the same thing whether you're iterating over a sequence or calling AMmapGet() or AMlistGet().

Calling AMitemResult() on an AMitem struct will produce a new AMresult struct referencing its storage so now the AMresult struct for an iterator can be subsequently freed without affecting the AMitem structs that were filtered out of it.

The storage for a set of AMitem structs can be recombined into a single AMresult struct by passing pointers to their corresponding AMresult structs to AMresultCat().

For C/C++ programmers, I've added AMstrCmp(), AMstrdup(), AM{idxType,objType,status,valType}ToString() and AM{idxType,objType,status,valType}FromString(). It's also now possible to pass arbitrary parameters through AMstack{Item,Items,Result}() to a callback function.
2023-02-25 18:47:00 +00:00

42 lines
1.3 KiB
C

#ifndef TESTS_CMOCKA_UTILS_H
#define TESTS_CMOCKA_UTILS_H
#include <stdlib.h>
#include <string.h>
/* third-party */
#include <automerge-c/utils/string.h>
#include <cmocka.h>
/* local */
#include "base_state.h"
/**
* \brief Forces the test to fail immediately and quit, printing the reason.
*
* \param[in] msg A message string into which \p view.src is interpolated.
* \param[in] view A UTF-8 string view as an `AMbyteSpan` struct.
*/
#define fail_msg_view(msg, view) \
do { \
char* const c_str = AMstrdup(view, NULL); \
print_error("ERROR: " msg "\n", c_str); \
free(c_str); \
fail(); \
} while (0)
/**
* \brief Validates the top result in a stack based upon the parameters
* specified within the given data structure and reports violations
* using cmocka assertions.
*
* \param[in,out] stack A pointer to a pointer to an `AMstack` struct.
* \param[in] data A pointer to an owned `AMpushData` struct.
* \return `true` if the top `AMresult` struct in \p stack is valid, `false`
* otherwise.
* \pre \p stack `!= NULL`.
* \pre \p data `!= NULL`.
*/
bool cmocka_cb(AMstack** stack, void* data);
#endif /* TESTS_CMOCKA_UTILS_H */