From dee5dd549cbf3e282e2603ca1d303759fff7f1df Mon Sep 17 00:00:00 2001 From: Alex Groce Date: Sun, 30 Dec 2018 21:04:15 -0700 Subject: [PATCH] C++ functions that also vary string length, using Pump --- src/include/deepstate/DeepState.h | 4 ++-- src/include/deepstate/DeepState.hpp | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/include/deepstate/DeepState.h b/src/include/deepstate/DeepState.h index aa57282..5a33970 100644 --- a/src/include/deepstate/DeepState.h +++ b/src/include/deepstate/DeepState.h @@ -145,12 +145,12 @@ DEEPSTATE_INLINE static int8_t DeepState_MaxChar(int8_t v) { * is true, the string will be null-terminated at size+1, and no prior character * will be null. For null-terminated strings, storage must have space for the * null terminator, so size should be 1 less than actual size. */ -extern void DeepState_AssignString(char *dest, size_t max_size, const char* allowed, +extern void DeepState_AssignString(char *dest, size_t size, const char* allowed, size_t allowed_size, int null_terminated); /* Returns a null-terminated string of size characters (before null), allocated on * heap. DeepState will handle freeing these strings at termination of the test. */ -extern char* DeepState_String(size_t max_size); +extern char* DeepState_String(size_t size); /* Function to clean up generated strings, and any other DeepState-managed data. */ extern void DeepState_CleanUp(); diff --git a/src/include/deepstate/DeepState.hpp b/src/include/deepstate/DeepState.hpp index e4c0617..03d14c2 100644 --- a/src/include/deepstate/DeepState.hpp +++ b/src/include/deepstate/DeepState.hpp @@ -314,7 +314,7 @@ static T Pump(T val, unsigned max=10) { } return Minimize(val); } - + template inline static void ForAll(void (*func)(Args...)) { func(Symbolic()...); @@ -345,7 +345,7 @@ inline static char OneOf(const char *str) { } return str[DeepState_IntInRange(0, strlen(str) - 1)]; } - + template inline static const T &OneOf(const std::vector &arr) { if (arr.empty()) { @@ -471,6 +471,25 @@ struct Comparer { } }; +/* Given a char pointer (assumed to have enough storage), places a C string + * value in that location, with up to max_size characters (strlen, not counting + * null). If allowed is non-null, chooses characters from allowed char array. + * If null_terminated is true, the string will be null-terminated at size+1, and + * no prior character will be null. For null-terminated strings, storage must have + * space for the null terminator, so max_size should be 1 less than actual size. */ +inline static void DeepState_AssignCString(char *dest, size_t max_size, const char* allowed, + size_t allowed_size, int null_terminated) { + uint32_t size = DeepState_UIntInRange(0, max_size); + DeepState_AssignString(dest, Pump(size, max_size), allowed, allowed_size, null_terminated); +} + +/* Returns a null-terminated C string of up to max_size characters (strlen), allocated on + * heap. DeepState will handle freeing these strings at termination of the test. */ +inline static char* DeepState_CString(size_t max_size) { + uint32_t size = DeepState_UIntInRange(0, max_size); + return DeepState_String(Pump(size, max_size)); +} + } // namespace deepstate #define ONE_OF ::deepstate::OneOf