Permalink
Browse files

All functions now accept IoLists as well

  • Loading branch information...
1 parent 97d1aa2 commit 36dd21fd2a0887578567c20a39dba78fe2a1fcb3 @fdmanana fdmanana committed Mar 27, 2011
Showing with 30 additions and 18 deletions.
  1. +4 −4 c_src/snappy.cc
  2. +4 −4 src/snappy.erl
  3. +22 −10 test/snappy_tests.erl
View
@@ -84,7 +84,7 @@ extern "C" {
ERL_NIF_TERM snappy_compress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ErlNifBinary input;
- if (!enif_inspect_binary(env, argv[0], &input)) {
+ if (!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
return enif_make_badarg(env);
}
@@ -110,7 +110,7 @@ extern "C" {
ERL_NIF_TERM snappy_decompress(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ErlNifBinary input;
- if (!enif_inspect_binary(env, argv[0], &input)) {
+ if (!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
return enif_make_badarg(env);
}
@@ -151,7 +151,7 @@ extern "C" {
ERL_NIF_TERM snappy_get_uncompressed_length(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ErlNifBinary input;
- if (!enif_inspect_binary(env, argv[0], &input)) {
+ if (!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
return enif_make_badarg(env);
}
@@ -174,7 +174,7 @@ extern "C" {
ERL_NIF_TERM snappy_is_valid_compressed_buffer(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ErlNifBinary input;
- if (!enif_inspect_binary(env, argv[0], &input)) {
+ if (!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
return enif_make_badarg(env);
}
View
@@ -37,17 +37,17 @@ init() ->
erlang:load_nif(SoName, 0).
-compress(_Bin) ->
+compress(_IoList) ->
exit({snappy_nif_not_loaded, module, ?MODULE, line, ?LINE}).
-decompress(_Bin) ->
+decompress(_IoList) ->
exit({snappy_nif_not_loaded, module, ?MODULE, line, ?LINE}).
-get_uncompressed_length(_Bin) ->
+get_uncompressed_length(_IoList) ->
exit({snappy_nif_not_loaded, module, ?MODULE, line, ?LINE}).
-is_valid_compressed_buffer(_Bin) ->
+is_valid_compressed_buffer(_IoList) ->
exit({snappy_nif_not_loaded, module, ?MODULE, line, ?LINE}).
View
@@ -25,8 +25,8 @@ decompression_test_() ->
compression() ->
- Data = iolist_to_binary(
- lists:duplicate(11, <<"words that go unspoken, deeds that go undone">>)),
+ DataIoList = lists:duplicate(11, <<"words that go unspoken, deeds that go undone">>),
+ Data = iolist_to_binary(DataIoList),
Result = snappy:compress(Data),
?assertMatch({ok, _}, Result),
{ok, Compressed} = Result,
@@ -36,27 +36,39 @@ compression() ->
?assertEqual(true, snappy:is_valid_compressed_buffer(Compressed)),
?assertEqual(false, snappy:is_valid_compressed_buffer(Data)),
?assertEqual(false, snappy:is_valid_compressed_buffer(<<"foobar123">>)),
-
?assertEqual({ok, byte_size(Data)}, snappy:get_uncompressed_length(Compressed)),
+
+ Result2 = snappy:compress(DataIoList),
+ ?assertMatch({ok, _}, Result2),
+ {ok, Compressed2} = Result2,
+
+ ?assertEqual(byte_size(Compressed2), byte_size(Compressed)),
+ ?assertEqual(true, snappy:is_valid_compressed_buffer(Compressed2)),
+ ?assertEqual({ok, byte_size(Data)}, snappy:get_uncompressed_length(Compressed2)),
ok.
decompression() ->
- Data = iolist_to_binary(
- lists:duplicate(11, <<"words that go unspoken, deeds that go undone">>)),
+ DataIoList = lists:duplicate(11, <<"words that go unspoken, deeds that go undone">>),
+ Data = iolist_to_binary(DataIoList),
Result = snappy:compress(Data),
?assertMatch({ok, _}, Result),
{ok, Compressed} = Result,
-
?assertEqual({ok, Data}, snappy:decompress(Compressed)),
+ Result2 = snappy:compress(DataIoList),
+ ?assertMatch({ok, _}, Result2),
+ {ok, Compressed2} = Result2,
+ ?assertEqual({ok, Data}, snappy:decompress(Compressed2)),
+
BigData = <<"mVPZzfDzKNeZrh1QdkMEgh2U0Bv2i3+bLJaCqgNibXuMuwfjrqTuxPGupxjI",
"xEbuYR+u/KZvSDhoxnkpPbgJo7oiQv2ibDrrGZx7RDs3Nn7Ww51B7+zUL4tr",
"G+16TlJilJT47Z4cQn8EpWex2bMRFAoJ6AMJAodLGbiD78yUyIorRKVcCa+k",
"udzjsqYAoXzW/z8JCB6rbGGSbnLyqztR//ch5sRwSvYARlV+IamzBkDXFZxj",
"5TAwAl2ZcbCeMX0qgXX4EonVZxc=">>,
- Result2 = snappy:compress(BigData),
- ?assertMatch({ok, _}, Result2),
- {ok, Compressed2} = Result2,
- ?assertEqual({ok, BigData}, snappy:decompress(Compressed2)),
+ Result3 = snappy:compress(BigData),
+ ?assertMatch({ok, _}, Result3),
+ {ok, Compressed3} = Result3,
+ ?assertEqual({ok, BigData}, snappy:decompress(Compressed3)),
ok.
+

0 comments on commit 36dd21f

Please sign in to comment.