<commit_before>#!/usr/bin/env escript

dbname() -> "test/dbs/004".
num_writes() -> 1000.

main(_) ->
    code:add_pathz("test"),
    code:add_pathz("ebin"),
    etap:plan(5),
    os:cmd("rm -rf " ++ dbname()),
    {ok, Db} = erleveldb:open_db(dbname(), [create_if_missing]),
    etap:is(test_write_kvs(Db, num_writes()), ok, "Wrote k/v pairs."),
    etap:is(test_seek_forward(Db), ok, "Seeked forward."),
    etap:is(test_seek_reverse(Db), ok, "Seeked reverse."),
    etap:is(test_seek_key(Db), ok, "Seeked to key."),
    etap:is(test_iter_opts(Db), ok, "Iterators can be created with options."),
    etap:end_tests().

test_write_kvs(_Db, 0) ->
    ok;
test_write_kvs(Db, N) ->
    K = to_key(N),
    ok = erleveldb:put(Db, K, K),
    test_write_kvs(Db, N-1).

test_seek_forward(Db) ->
    {ok, Iter} = erleveldb:iter(Db),
    {<<"000001">>, <<"000001">>} = erleveldb:seek(Iter, first),
    lists:foreach(fun(N) ->
        Next = {to_key(N), to_key(N)},
        Next = erleveldb:next(Iter)
    end, lists:seq(2, num_writes())),
    ok.

test_seek_reverse(Db) ->
    {ok, Iter} = erleveldb:iter(Db),
    {<<"001000">>, <<"001000">>} = erleveldb:seek(Iter, last),
    lists:foreach(fun(N) ->
        Next = {to_key(N), to_key(N)},
        Next = erleveldb:prev(Iter)
    end, lists:seq(num_writes()-1, 1, -1)),
    ok.

test_seek_key(Db) ->
    {ok, Iter} = erleveldb:iter(Db),
    lists:foreach(fun(N) ->
        Next = {to_key(N), to_key(N)},
        Next = erleveldb:seek(Iter, to_key(N))
    end, lists:seq(1, num_writes(), 5)),
    ok.

test_iter_opts(Db) ->
    {ok, _} = erleveldb:iter(Db, []),
    {ok, _} = erleveldb:iter(Db, [verify_checksums]),
    {ok, _} = erleveldb:iter(Db, [skip_cache]),
    {ok, _} = erleveldb:iter(Db, [verify_checksums, skip_cache]),
    ok = is_badarg(catch erleveldb:iter(Db, bing)),
    ok = is_badarg(catch erleveldb:iter(Db, [2])),
    ok = is_badarg(catch erleveldb:iter(Db, [{foo,bar}])),
    ok.

to_key(N) when is_integer(N) ->
    iolist_to_binary(io_lib:format("~6.10.0b", [N])).

is_badarg({'EXIT', {badarg, _}}) ->
    ok.<commit_msg>Explicit check for end of iterator.<commit_after>#!/usr/bin/env escript

dbname() -> "test/dbs/004".
num_writes() -> 1000.

main(_) ->
    code:add_pathz("test"),
    code:add_pathz("ebin"),
    etap:plan(5),
    os:cmd("rm -rf " ++ dbname()),
    {ok, Db} = erleveldb:open_db(dbname(), [create_if_missing]),
    etap:is(test_write_kvs(Db, num_writes()), ok, "Wrote k/v pairs."),
    etap:is(test_seek_forward(Db), ok, "Seeked forward."),
    etap:is(test_seek_reverse(Db), ok, "Seeked reverse."),
    etap:is(test_seek_key(Db), ok, "Seeked to key."),
    etap:is(test_iter_opts(Db), ok, "Iterators can be created with options."),
    etap:end_tests().

test_write_kvs(_Db, 0) ->
    ok;
test_write_kvs(Db, N) ->
    K = to_key(N),
    ok = erleveldb:put(Db, K, K),
    test_write_kvs(Db, N-1).

test_seek_forward(Db) ->
    {ok, Iter} = erleveldb:iter(Db),
    {<<"000001">>, <<"000001">>} = erleveldb:seek(Iter, first),
    lists:foreach(fun(N) ->
        Next = {to_key(N), to_key(N)},
        Next = erleveldb:next(Iter)
    end, lists:seq(2, num_writes())),
    not_found = erleveldb:next(Iter),
    ok.

test_seek_reverse(Db) ->
    {ok, Iter} = erleveldb:iter(Db),
    {<<"001000">>, <<"001000">>} = erleveldb:seek(Iter, last),
    lists:foreach(fun(N) ->
        Next = {to_key(N), to_key(N)},
        Next = erleveldb:prev(Iter)
    end, lists:seq(num_writes()-1, 1, -1)),
    not_found = erleveldb:prev(Iter),
    ok.

test_seek_key(Db) ->
    {ok, Iter} = erleveldb:iter(Db),
    lists:foreach(fun(N) ->
        Next = {to_key(N), to_key(N)},
        Next = erleveldb:seek(Iter, to_key(N))
    end, lists:seq(1, num_writes(), 5)),
    ok.

test_iter_opts(Db) ->
    {ok, _} = erleveldb:iter(Db, []),
    {ok, _} = erleveldb:iter(Db, [verify_checksums]),
    {ok, _} = erleveldb:iter(Db, [skip_cache]),
    {ok, _} = erleveldb:iter(Db, [verify_checksums, skip_cache]),
    ok = is_badarg(catch erleveldb:iter(Db, bing)),
    ok = is_badarg(catch erleveldb:iter(Db, [2])),
    ok = is_badarg(catch erleveldb:iter(Db, [{foo,bar}])),
    ok.

to_key(N) when is_integer(N) ->
    iolist_to_binary(io_lib:format("~6.10.0b", [N])).

is_badarg({'EXIT', {badarg, _}}) ->
    ok.
{'fraction_non_alphanumeric': 0.20999530736743313, 'fraction_numerical': 0.02111684655091506, 'mean_word_length': 3.7209302325581395, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3182602', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1815, 'n_words': 298}