Skip to content

Commit dd729ef

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

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,35 @@ luaL_remaining_bytes(lua_State *L)
231231
return 1;
232232
}
233233

234+
/* Returns a random element of specified array. */
235+
static int
236+
luaL_oneof(lua_State *L)
237+
{
238+
lua_userdata_t *lfdp;
239+
lfdp = (lua_userdata_t *)luaL_checkudata(L, 1, FDP_LUA_UDATA_NAME);
240+
if (!lfdp)
241+
luaL_error(L, "Usage: <FuzzedDataProvider>:oneof(table)");
242+
luaL_checktype(L, 2, LUA_TTABLE);
243+
244+
int len = 0;
245+
/* Push starting `nil` for iterations. */
246+
lua_pushnil(L);
247+
while (lua_next(L, 2) != 0) {
248+
/*
249+
* Remove `value` from the stack. Keeps `key` for
250+
* the next iteration.
251+
*/
252+
lua_pop(L, 1);
253+
len++;
254+
}
255+
256+
int idx = lfdp->fdp->ConsumeIntegralInRange(1, len);
257+
lua_pushinteger(L, idx);
258+
lua_gettable(L, -2);
259+
260+
return 1;
261+
}
262+
234263
static int close(lua_State *L) {
235264
lua_userdata_t *lfdp;
236265
lfdp = (lua_userdata_t *)luaL_checkudata(L, 1, FDP_LUA_UDATA_NAME);
@@ -256,6 +285,7 @@ const luaL_Reg methods[] =
256285
{ "consume_integers", luaL_consume_integers },
257286
{ "consume_probability", luaL_consume_probability },
258287
{ "remaining_bytes", luaL_remaining_bytes },
288+
{ "oneof", luaL_oneof },
259289
{ "__gc", close },
260290
{ "__tostring", tostring },
261291
{ NULL, NULL }

0 commit comments

Comments
 (0)