Skip to content

Commit 6ba4e50

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

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,33 @@ 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+
int idx = lfdp->fdp->ConsumeIntegralInRange(1, len);
232+
lua_pushinteger(L, idx);
233+
lua_gettable(L, -2);
234+
235+
return 1;
236+
}
237+
211238
static int close(lua_State *L) {
212239
lua_userdata_t *lfdp;
213240
lfdp = (lua_userdata_t *)luaL_checkudata(L, 1, FDP_LUA_UDATA_NAME);
@@ -233,6 +260,7 @@ const luaL_Reg methods[] =
233260
{ "consume_integers", luaL_consume_integers },
234261
{ "consume_probability", luaL_consume_probability },
235262
{ "remaining_bytes", luaL_remaining_bytes },
263+
{ "oneof", luaL_oneof },
236264
{ "__gc", close },
237265
{ "__tostring", tostring },
238266
{ NULL, NULL }

luzer/tests/test_unit.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ 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+
181+
local n = fdp:oneof({1, 2})
182+
assert(type(n) == "number" and (n == 1 or n == 2))
183+
177184
local function custom_mutator(data, size, max_size, seed)
178185
assert(type(data) == "string")
179186
assert(type(size) == "number")

0 commit comments

Comments
 (0)