Skip to content

Commit 48f80a8

Browse files
committed
luzer: introduce oneof() in FuzzedDataProvider
``` > fdp:oneof({'clickhouse', 'ydb', 'tarantool'}) --- - tarantool ... > ```
1 parent 8364c69 commit 48f80a8

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Examples with tests.
1616
- Documentation with usecases, API etc.
1717
- Support command-line options.
18+
- Method `oneof()` in FuzzedDataProvider.
1819

1920
### Changed
2021

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The `FuzzedDataProvider` then supports the following functions:
6868
If there's no input data left, always returns 0.
6969
- `remaining_bytes()` - returns the number of unconsumed bytes in the fuzzer
7070
input.
71+
- `oneof()` - returns a random element in specified Lua array.
7172

7273
Examples:
7374

luzer/fuzzed_data_provider.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,37 @@ luaL_remaining_bytes(lua_State *L)
208208
return 1;
209209
}
210210

211+
/* Returns a random element of specified array. */
212+
static int
213+
luaL_oneof(lua_State *L)
214+
{
215+
lua_userdata_t *lfdp;
216+
lfdp = (lua_userdata_t *)luaL_checkudata(L, 1, FDP_LUA_UDATA_NAME);
217+
luaL_checktype(L, 2, LUA_TTABLE);
218+
219+
int len = 0;
220+
/* Push starting `nil` for iterations. */
221+
lua_pushnil(L);
222+
while (lua_next(L, 2) != 0) {
223+
/*
224+
* Remove `value` from the stack. Keeps `key` for
225+
* the next iteration.
226+
*/
227+
lua_pop(L, 1);
228+
len++;
229+
}
230+
231+
if (len == 0) {
232+
luaL_error(L, "table is empty");
233+
}
234+
int idx = lfdp->fdp->ConsumeIntegralInRange(1, len);
235+
/* lua_pop(L, 2); */
236+
lua_pushinteger(L, idx);
237+
lua_gettable(L, -2);
238+
239+
return 1;
240+
}
241+
211242
static int close(lua_State *L) {
212243
lua_userdata_t *lfdp;
213244
lfdp = (lua_userdata_t *)luaL_checkudata(L, 1, FDP_LUA_UDATA_NAME);
@@ -233,6 +264,7 @@ const luaL_Reg methods[] =
233264
{ "consume_integers", luaL_consume_integers },
234265
{ "consume_probability", luaL_consume_probability },
235266
{ "remaining_bytes", luaL_remaining_bytes },
267+
{ "oneof", luaL_oneof },
236268
{ "__gc", close },
237269
{ "__tostring", tostring },
238270
{ NULL, NULL }

luzer/tests/test_unit.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ assert(p1 >= 0 and p2 >= 0)
174174
assert(p1 <= 1 and p2 <= 1)
175175
assert(p1 ~= p2)
176176

177+
-- luzer.FuzzedDataProvider.oneof()
178+
fdp = luzer.FuzzedDataProvider("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
179+
assert(type(fdp.oneof) == "function")
180+
local n = fdp:oneof({1, 2})
181+
assert(type(n) == "number" and (n == 1 or n == 2))
182+
177183
local function custom_mutator(data, size, max_size, seed)
178184
assert(type(data) == "string")
179185
assert(type(size) == "number")

0 commit comments

Comments
 (0)