Permalink
Browse files

amf3 test cases

  • Loading branch information...
1 parent 37ec5e4 commit 7c3aecef98e6311da2341443f0f4dfd9dfe648aa @trung committed Oct 7, 2009
Showing with 63 additions and 2 deletions.
  1. +3 −1 src/amf0.erl
  2. +27 −1 src/amf3.erl
  3. +33 −0 test/amf3_test.erl
View
@@ -231,7 +231,9 @@ write_object(Obj) ->
end.
write_object(ref, Ref) -> write_reference(Ref);
-write_object(amf3, Obj) -> amf3:write_object(Obj).
+write_object(amf3, Obj) ->
+ {ok, Amf3Bin} = amf3:write_object(Obj),
+ write_object_now(?avm_plus_object_marker, Amf3Bin).
write_object_info(Field, Obj) when is_record(Field, string) ->
{ok, FieldBin} = string_to_binary(Field),
View
@@ -47,7 +47,7 @@ read_uint_29(<<2#1:1, First:7, 2#0:1, Second:7, Rest/binary>>) ->
read_uint_29(<<2#1:1, First:7, 2#1:1, Second:7, 2#0:1, Third:7, Rest/binary>>) ->
{ok, ((((First band 16#7F) bsl 7) bor (Second band 16#7F)) bsl 7) bor Third, Rest};
read_uint_29(<<2#1:1, First:7, 2#1:1, Second:7, 2#1:1, Third:7, Forth:8, Rest/binary>>) ->
- {ok, (((((First band 16#7F) bsl 7) bor (Second band 16#7F)) bsl 7) bor ((Third band 16#7F) bsl 8)) bor Forth, Rest};
+ {ok, ((((((First band 16#7F) bsl 7) bor (Second band 16#7F)) bsl 7) bor (Third band 16#7F)) bsl 8) bor Forth, Rest};
read_uint_29(_) ->
{bad, "Not a binary or number out of range or empty binary"}.
@@ -308,3 +308,29 @@ read_object(<<>>) ->
read_object(Something) ->
{bad, {"Not a binary or not matched any marker", Something}}.
+
+%% ===============================================
+%% Writers
+%% ===============================================
+
+write_null() ->
+ {ok, <<?null_marker>>}.
+
+write_uint_29(Value) when Value < 16#80 -> amf0:write_u8(Value);
+write_uint_29(Value) when Value < 16#4000 ->
+ {ok, FirstBin} = amf0:write_u8(((Value bsr 7) band 16#7F) bor 16#80),
+ {ok, SecondBin} = amf0:write_u8(Value band 16#7F),
+ {ok, list_to_binary([FirstBin, SecondBin])};
+write_uint_29(Value) when Value < 16#200000 ->
+ {ok, FirstBin} = amf0:write_u8(((Value bsr 14) band 16#7F) bor 16#80),
+ {ok, SecondBin} = amf0:write_u8(((Value bsr 7) band 16#7F) bor 16#80),
+ {ok, ThirdBin} = amf0:write_u8(Value band 16#7F),
+ {ok, list_to_binary([FirstBin, SecondBin, ThirdBin])};
+write_uint_29(Value) when Value < 16#40000000 ->
+ {ok, FirstBin} = amf0:write_u8(((Value bsr 22) band 16#7F) bor 16#80),
+ {ok, SecondBin} = amf0:write_u8(((Value bsr 15) band 16#7F) bor 16#80),
+ {ok, ThirdBin} = amf0:write_u8(((Value bsr 8) band 16#7F) bor 16#80),
+ {ok, ForthBin} = amf0:write_u8(Value band 16#FF),
+ {ok, list_to_binary([FirstBin, SecondBin, ThirdBin, ForthBin])};
+write_uint_29(Value) ->
+ {bad, {"Value out of range", Value, ?MODULE, ?LINE}}.
View
@@ -0,0 +1,33 @@
+-module(amf3_test).
+-author("trung@mdkt.org").
+
+-include_lib("eunit/include/eunit.hrl").
+-include("../include/types.hrl").
+-include("../include/messages.hrl").
+-compile(export_all).
+
+uint29_verify(ExpectedValue) ->
+ {ok, Bin} = amf3:write_uint_29(ExpectedValue),
+ ?assert(Bin /= <<>>),
+ {ok, ActualValue, _} = amf3:read_uint_29(Bin),
+ ?assertEqual(ExpectedValue, ActualValue).
+
+uint29_less_128_test() ->
+ ExpectedValue = 123,
+ uint29_verify(ExpectedValue).
+
+uint29_less_16384_test() ->
+ ExpectedValue = 15384,
+ uint29_verify(ExpectedValue).
+
+uint29_less_2097152_test() ->
+ ExpectedValue = 1097152,
+ uint29_verify(ExpectedValue).
+
+uint29_less_1073741824_test() ->
+ ExpectedValue = 1023741824,
+ uint29_verify(ExpectedValue).
+
+uint29_out_of_range_test() ->
+ ExpectedValue = 1073741824,
+ {bad, _} = amf3:write_uint_29(ExpectedValue).

0 comments on commit 7c3aece

Please sign in to comment.