From 881903faee8a918ad1e00c67258448c41c7bfee8 Mon Sep 17 00:00:00 2001 From: AICells <1713699517@qq.com> Date: Thu, 16 Jul 2020 22:38:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/protoCode.erl | 21 +- src/protoGen.erl | 32 +- test/V2/protoMsg.erl | 851 +++++++++++++++++++++++++++++++++++++++++++ test/V2/protoMsg.hrl | 226 ++++++++++++ test/V2/test2.erl | 672 ++++++++++++++++++++++++++++++++++ test/protoMsg.erl | 99 ++--- test/test2.erl | 672 ++++++++++++++++++++++++++++++++++ test/v1/protoMsg.erl | 842 ++++++++++++++++++++++++++++++++++++++++++ test/v1/protoMsg.hrl | 226 ++++++++++++ test/v1/start.bat | 1 + test/v1/test1.erl | 672 ++++++++++++++++++++++++++++++++++ 11 files changed, 4253 insertions(+), 61 deletions(-) create mode 100644 test/V2/protoMsg.erl create mode 100644 test/V2/protoMsg.hrl create mode 100644 test/V2/test2.erl create mode 100644 test/test2.erl create mode 100644 test/v1/protoMsg.erl create mode 100644 test/v1/protoMsg.hrl create mode 100644 test/v1/start.bat create mode 100644 test/v1/test1.erl diff --git a/src/protoCode.erl b/src/protoCode.erl index 4675720..c370bad 100644 --- a/src/protoCode.erl +++ b/src/protoCode.erl @@ -1,7 +1,7 @@ -module(protoCode). -compile([export_all, nowarn_unused_function, nowarn_export_all]). --export([encode/1, decode/1, encodeRec/1, decodeBin/2]). +-export([encodeIol/1, encodeBin/1, encodeIol/2, subEncode/1, subEncode/2, decode/1, decodeBin/2]). -define(min8, -128). -define(max8, 127). @@ -31,7 +31,7 @@ -define(float(V), <>). -define(double(V), <>). -define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)). --define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)). +-define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, subEncode(V)] end)). -define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]). -define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]). -define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]). @@ -46,7 +46,7 @@ -define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]). -define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]). -define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]). --define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]). +-define(list_record(List), [<<(length(List)):16/big>>, [subEncode(V) || V <- List]]). -define(BinaryShareSize, 65). %% binary 大于64时 binary和sub就会share -define(BinaryCopyRatio, 1.2). %% 当总binary的Sise / Sub binary size > 1.2 就重新复制一个 @@ -148,17 +148,24 @@ deRecordList(N, MsgId, MsgBin, RetList) -> {Tuple, LeftBin} = decodeRec(MsgId, MsgBin), deRecordList(N - 1, MsgId, LeftBin, [Tuple | RetList]). +encodeIol(RecMsg) -> + encodeIol(erlang:element(1, RecMsg), RecMsg). + +encodeBin(RecMsg) -> + erlang:iolist_to_binary(encodeIol(RecMsg)). + +subEncode(RecMsg) -> + subEncode(erlang:element(1, RecMsg), RecMsg). + %%%%%%%%%%%%%%%%%%%%%%%%%% 防止编译报错占位 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -encode(_) -> +encodeIol(_, _) -> ok. -encodeRec(_) -> +subEncode(_, _) -> ok. decodeBin(_, _) -> ok. decodeRec(_, _) -> ok. -getMsgId(_) -> - ok. diff --git a/src/protoGen.erl b/src/protoGen.erl index 873fdfb..d230b3b 100644 --- a/src/protoGen.erl +++ b/src/protoGen.erl @@ -23,7 +23,7 @@ protoErlHeader() -> "-module(protoMsg).\n\n -compile([nowarn_unused_vars]). --export([encode/1, decode/1, encodeRec/1, decodeBin/2]). +-export([encodeIol/1, encodeBin/1, encodeIol/2, subEncode/1, subEncode/2, decode/1, decodeBin/2]). -define(min8, -128). -define(max8, 127). @@ -53,7 +53,7 @@ protoErlHeader() -> -define(float(V), <>). -define(double(V), <>). -define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)). --define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)). +-define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, subEncode(V)] end)). -define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]). -define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]). -define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]). @@ -68,7 +68,7 @@ protoErlHeader() -> -define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]). -define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]). -define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]). --define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]). +-define(list_record(List), [<<(length(List)):16/big>>, [subEncode(V) || V <- List]]). -define(BinaryShareSize, 65). -define(BinaryCopyRatio, 1.2). @@ -168,7 +168,16 @@ deRecordList(0, _MsgId, MsgBin, RetList) -> {lists:reverse(RetList), MsgBin}; deRecordList(N, MsgId, MsgBin, RetList) -> {Tuple, LeftBin} = decodeRec(MsgId, MsgBin), - deRecordList(N - 1, MsgId, LeftBin, [Tuple | RetList]).\n\n". + deRecordList(N - 1, MsgId, LeftBin, [Tuple | RetList]). + +encodeIol(RecMsg) -> + encodeIol(erlang:element(1, RecMsg), RecMsg). + +encodeBin(RecMsg) -> + erlang:iolist_to_binary(encodeIol(RecMsg)). + +subEncode(RecMsg) -> + subEncode(erlang:element(1, RecMsg), RecMsg).\n\n". genMsgHrl(FieldInfo, {Index, Len, AccList}) -> TemStr = @@ -195,9 +204,9 @@ genEncodeRec({MsgName, MsgId, FieldList}, IsForBin) -> HeadStr = case IsForBin of true -> - "encode({" ++ MsgName ++ TemStr; + "encodeIol(" ++ MsgName ++ ", {_" ++ TemStr; _ -> - "encodeRec({" ++ MsgName ++ TemStr + "subEncode(" ++ MsgName ++ ", {_" ++ TemStr end, FunBody = @@ -694,7 +703,7 @@ convertDir(ProtoDir, HrlDir, ErlDir) -> {[HrlStr | MsgHrlAcc], ["Unuse"], [EncodeStr | MsgEncodeAcc], [DecodeStr | MsgDecodeAcc]} end, - {MsgHrlStr, _MsgIdStr, MsgEncodeStr, MsgDecodeStr} = lists:foldl(FunSpell, {[], ["getMsgId(_) -> 0.\n\n"], ["encode(_) ->\n\t[].\n\n"], ["decodeBin(_, _) ->\n\t{{}, <<>>}.\n\n"]}, SortedSProtoList), + {MsgHrlStr, _MsgIdStr, MsgEncodeStr, MsgDecodeStr} = lists:foldl(FunSpell, {[], ["getMsgId(_) -> 0.\n\n"], ["encodeIol(_, _) ->\n\t[].\n\n"], ["decodeBin(_, _) ->\n\t{{}, <<>>}.\n\n"]}, SortedSProtoList), SortedErrList = lists:sort(fun({_ErrName1, ErrCodeId1, _Desc1}, {_ErrName2, ErrCodeId2, _Desc2}) -> ErrCodeId1 > ErrCodeId2 end, ErrCodeList), @@ -712,7 +721,7 @@ convertDir(ProtoDir, HrlDir, ErlDir) -> DecodeStr = genDecodeBin(MsgInfo, SortedSProtoList, false), {[EncodeStr | SubEncodeAcc], [DecodeStr | SubDecodeAcc]} end, - {MsgEncodeRecStr, MsgDecodeRecStr} = lists:foldl(FunSubRec, {["encodeRec(_) ->\n\t[].\n\n"], ["decodeRec(_, _) ->\n\t{{}, <<>>}.\n\n"]}, SortedSubRecList), + {MsgEncodeRecStr, MsgDecodeRecStr} = lists:foldl(FunSubRec, {["subEncode(_, _) ->\n\t[].\n\n"], ["decodeRec(_, _) ->\n\t{{}, <<>>}.\n\n"]}, SortedSubRecList), ErlHeaderStr = protoErlHeader(), OutputStr = ErlHeaderStr ++ MsgEncodeRecStr ++ MsgEncodeStr ++ MsgDecodeRecStr ++ MsgDecodeStr, @@ -732,6 +741,11 @@ do_write_hrl(OutDir, Mod, Str) when is_list(OutDir) -> do_write_erl(OutDir, Mod, Str) when is_list(OutDir) -> Filename = filename:join([OutDir, atom_to_list(Mod) ++ ".erl"]), - ok = file:write_file(Filename, Str), + case file:write_file(Filename, Str) of + ok -> + ok; + _Ret -> + io:format("write to erl file error:~p ~n", [_Ret]) + end, Filename. diff --git a/test/V2/protoMsg.erl b/test/V2/protoMsg.erl new file mode 100644 index 0000000..1ddf14f --- /dev/null +++ b/test/V2/protoMsg.erl @@ -0,0 +1,851 @@ +-module(protoMsg). + + +-compile([nowarn_unused_vars]). + +-export([encodeIol/1, encodeBin/1, encodeIol/2, subEncode/1, subEncode/2, decode/1, decodeBin/2]). + +-define(min8, -128). +-define(max8, 127). +-define(min16, -32768). +-define(max16, 32767). +-define(min32, -2147483648). +-define(max32, 2147483647). +-define(min64, -9223372036854775808). +-define(max64, 9223372036854775807). + +-define(minF32, -3.4E+38). +-define(maxF32, 3.4E+38). +-define(minF64, -1.797E-308). +-define(maxF64, 1.797E+308). + +-define(int8(V), <>). +-define(uint8(V), <>). +-define(int16(V), <>). +-define(uint16(V), <>). +-define(int32(V), <>). +-define(uint32(V), <>). +-define(int64(V), <>). +-define(uint64(V), <>). +-define(integer(V), (integer(V))). +-define(number(V), (number(V))). +-define(string(V), (string(V))). +-define(float(V), <>). +-define(double(V), <>). +-define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)). +-define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, subEncode(V)] end)). +-define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]). +-define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]). +-define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]). +-define(list_int16(List), [<<(length(List)):16/big>>, [?int16(V) || V <- List]]). +-define(list_uint16(List), [<<(length(List)):16/big>>, [?uint16(V) || V <- List]]). +-define(list_int32(List), [<<(length(List)):16/big>>, [?int32(V) || V <- List]]). +-define(list_uint32(List), [<<(length(List)):16/big>>, [?uint32(V) || V <- List]]). +-define(list_int64(List), [<<(length(List)):16/big>>, [?int64(V) || V <- List]]). +-define(list_uint64(List), [<<(length(List)):16/big>>, [?uint64(V) || V <- List]]). +-define(list_float(List), [<<(length(List)):16/big>>, [?float(V) || V <- List]]). +-define(list_double(List), [<<(length(List)):16/big>>, [?double(V) || V <- List]]). +-define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]). +-define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]). +-define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]). +-define(list_record(List), [<<(length(List)):16/big>>, [subEncode(V) || V <- List]]). + +-define(BinaryShareSize, 65). +-define(BinaryCopyRatio, 1.2). + +integer(V) -> + if + V >= ?min8 andalso V =< ?max8 -> + <<8:8, <>/binary>>; + V >= ?min16 andalso V =< ?max16 -> + <<16:8, <>/binary>>; + V >= ?min32 andalso V =< ?max32 -> + <<32:8, <>/binary>>; + V >= ?min64 andalso V =< ?max64 -> + <<64:8, <>/binary>>; + true -> + throw(exceeded_the_integer) + end. + +number(V) -> + if + erlang:is_integer(V) -> + if + V >= ?min8 andalso V =< ?max8 -> + <<8:8, <>/binary>>; + V >= ?min16 andalso V =< ?max16 -> + <<16:8, <>/binary>>; + V >= ?min32 andalso V =< ?max32 -> + <<32:8, <>/binary>>; + V >= ?min64 andalso V =< ?max64 -> + <<64:8, <>/binary>>; + true -> + throw(exceeded_the_integer) + end; + erlang:is_float(V) -> + if + V >= ?minF32 andalso V =< ?maxF32 -> + <<33:8, <>/binary>>; + V >= ?minF64 andalso V =< ?maxF64 -> + <<65:8, <>/binary>>; + true -> + throw(exceeded_the_float) + end; + true -> + throw(is_not_number) + end. + +string(Str) when is_binary(Str) -> + [<<(byte_size(Str)):16/big>>, Str]; +string(Str) -> + Str2 = unicode:characters_to_binary(Str, utf8), + [<<(byte_size(Str2)):16/big>>, Str2]. + +decode(Bin) -> + <> = Bin, + decodeBin(MsgId, MsgBin). + +deIntegerList(0, MsgBin, RetList) -> + {lists:reverse(RetList), MsgBin}; +deIntegerList(N, MsgBin, RetList) -> + <> = MsgBin, + deIntegerList(N - 1, LeftBin, [Int | RetList]). + +deNumberList(0, MsgBin, RetList) -> + {lists:reverse(RetList), MsgBin}; +deNumberList(N, MsgBin, RetList) -> + <> = MsgBin, + case NumBits of + 33 -> + <> = NumBin, + deNumberList(N - 1, LeftBin, [Float | RetList]); + 65 -> + <> = NumBin, + deNumberList(N - 1, LeftBin, [Float | RetList]); + _ -> + <> = NumBin, + deNumberList(N - 1, LeftBin, [Int | RetList]) + end. + +deStringList(0, MsgBin, _RefSize, RetList) -> + {lists:reverse(RetList), MsgBin}; +deStringList(N, MsgBin, RefSize, RetList) -> + <> = MsgBin, + case Len < ?BinaryShareSize of + true -> + deStringList(N - 1, LeftBin, RefSize, [StrBin | RetList]); + _ -> + case RefSize / Len > ?BinaryCopyRatio of + true -> + StrBinCopy = binary:copy(StrBin), + deStringList(N - 1, LeftBin, RefSize, [StrBinCopy | RetList]); + _ -> + deStringList(N - 1, LeftBin, RefSize, [StrBin | RetList]) + end + end. + +deRecordList(0, _MsgId, MsgBin, RetList) -> + {lists:reverse(RetList), MsgBin}; +deRecordList(N, MsgId, MsgBin, RetList) -> + {Tuple, LeftBin} = decodeRec(MsgId, MsgBin), + deRecordList(N - 1, MsgId, LeftBin, [Tuple | RetList]). + +encodeIol(RecMsg) -> + encodeIol(erlang:element(1, RecMsg), RecMsg). + +encodeBin(RecMsg) -> + erlang:iolist_to_binary(encodeIol(RecMsg)). + +subEncode(RecMsg) -> + subEncode(erlang:element(1, RecMsg), RecMsg). + +subEncode(test, {_, V1}) -> + [?string(V1)]; +subEncode(phoneNumber, {_, V1, V2}) -> + [?record(V1), ?int32(V2)]; +subEncode(person, {_, V1, V2, V3, V4}) -> + [?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; +subEncode(union, {_, V1, V2}) -> + [?string(V1), ?int32(V2)]; +subEncode(_, _) -> + []. + +encodeIol(test, {_, V1}) -> + [<<1:16/big-unsigned>>, ?string(V1)]; +encodeIol(phoneNumber, {_, V1, V2}) -> + [<<2:16/big-unsigned>>, ?record(V1), ?int32(V2)]; +encodeIol(person, {_, V1, V2, V3, V4}) -> + [<<3:16/big-unsigned>>, ?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; +encodeIol(addressBook, {_, V1, V2}) -> + [<<4:16/big-unsigned>>, ?list_record(V1), ?list_record(V2)]; +encodeIol(union, {_, V1, V2}) -> + [<<5:16/big-unsigned>>, ?string(V1), ?int32(V2)]; +encodeIol(tbool, {_, V1}) -> + [<<6:16/big-unsigned>>, ?bool(V1)]; +encodeIol(tint8, {_, V1, V2}) -> + [<<7:16/big-unsigned>>, ?int8(V1), ?int8(V2)]; +encodeIol(tuint8, {_, V1, V2}) -> + [<<8:16/big-unsigned>>, ?uint8(V1), ?uint8(V2)]; +encodeIol(tint16, {_, V1, V2}) -> + [<<9:16/big-unsigned>>, ?int16(V1), ?int16(V2)]; +encodeIol(tuint16, {_, V1, V2}) -> + [<<10:16/big-unsigned>>, ?uint16(V1), ?uint16(V2)]; +encodeIol(tint32, {_, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) -> + [<<11:16/big-unsigned>>, ?int32(V1), ?int32(V2), ?int32(V3), ?int32(V4), ?int32(V5), ?int32(V6), ?int32(V7), ?int32(V8), ?int32(V9), ?int32(V10)]; +encodeIol(tuint32, {_, V1, V2}) -> + [<<12:16/big-unsigned>>, ?uint32(V1), ?uint32(V2)]; +encodeIol(tint64, {_, V1, V2}) -> + [<<13:16/big-unsigned>>, ?int64(V1), ?int64(V2)]; +encodeIol(tuint64, {_, V1, V2}) -> + [<<14:16/big-unsigned>>, ?uint64(V1), ?uint64(V2)]; +encodeIol(tinteger, {_, V1, V2, V3, V4, V5, V6, V7, V8}) -> + [<<15:16/big-unsigned>>, ?integer(V1), ?integer(V2), ?integer(V3), ?integer(V4), ?integer(V5), ?integer(V6), ?integer(V7), ?integer(V8)]; +encodeIol(tnumber, {_, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) -> + [<<16:16/big-unsigned>>, ?number(V1), ?number(V2), ?number(V3), ?number(V4), ?number(V5), ?number(V6), ?number(V7), ?number(V8), ?number(V9), ?number(V10)]; +encodeIol(tfloat, {_, V1, V2}) -> + [<<17:16/big-unsigned>>, ?float(V1), ?float(V2)]; +encodeIol(tdouble, {_, V1, V2}) -> + [<<18:16/big-unsigned>>, ?double(V1), ?double(V2)]; +encodeIol(tstring, {_, V1, V2}) -> + [<<19:16/big-unsigned>>, ?string(V1), ?string(V2)]; +encodeIol(tlistbool, {_, V1}) -> + [<<20:16/big-unsigned>>, ?list_bool(V1)]; +encodeIol(tlistint8, {_, V1}) -> + [<<21:16/big-unsigned>>, ?list_int8(V1)]; +encodeIol(tlistuint8, {_, V1}) -> + [<<22:16/big-unsigned>>, ?list_uint8(V1)]; +encodeIol(tlistint16, {_, V1}) -> + [<<23:16/big-unsigned>>, ?list_int16(V1)]; +encodeIol(tlistuint16, {_, V1}) -> + [<<24:16/big-unsigned>>, ?list_uint16(V1)]; +encodeIol(tlistint32, {_, V1}) -> + [<<25:16/big-unsigned>>, ?list_int32(V1)]; +encodeIol(tlistuint32, {_, V1}) -> + [<<26:16/big-unsigned>>, ?list_uint32(V1)]; +encodeIol(tlistint64, {_, V1}) -> + [<<27:16/big-unsigned>>, ?list_int64(V1)]; +encodeIol(tlistuint64, {_, V1}) -> + [<<28:16/big-unsigned>>, ?list_uint64(V1)]; +encodeIol(tlistinteger, {_, V1}) -> + [<<29:16/big-unsigned>>, ?list_integer(V1)]; +encodeIol(tlistnumber, {_, V1}) -> + [<<30:16/big-unsigned>>, ?list_number(V1)]; +encodeIol(tlistfloat, {_, V1}) -> + [<<31:16/big-unsigned>>, ?list_float(V1)]; +encodeIol(tlistdouble, {_, V1}) -> + [<<32:16/big-unsigned>>, ?list_double(V1)]; +encodeIol(tliststring, {_, V1}) -> + [<<33:16/big-unsigned>>, ?list_string(V1)]; +encodeIol(tlistunion, {_, V1}) -> + [<<34:16/big-unsigned>>, ?list_record(V1)]; +encodeIol(allType, {_, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}) -> + [<<35:16/big-unsigned>>, ?bool(V1), ?int8(V2), ?uint8(V3), ?int16(V4), ?uint16(V5), ?int32(V6), ?uint32(V7), ?int64(V8), ?uint64(V9), ?integer(V10), ?integer(V11), ?integer(V12), ?integer(V13), ?integer(V14), ?integer(V15), ?integer(V16), ?integer(V17), ?number(V18), ?number(V19), ?number(V20), ?number(V21), ?number(V22), ?number(V23), ?number(V24), ?number(V25), ?number(V26), ?number(V27), ?float(V28), ?double(V29), ?string(V30), ?string(V31), ?record(V32), ?list_bool(V33), ?list_int8(V34), ?list_uint8(V35), ?list_int16(V36), ?list_uint16(V37), ?list_int32(V38), ?list_uint32(V39), ?list_int64(V40), ?list_uint64(V41), ?list_integer(V42), ?list_integer(V43), ?list_integer(V44), ?list_integer(V45), ?list_number(V46), ?list_number(V47), ?list_number(V48), ?list_number(V49), ?list_number(V50), ?list_number(V51), ?list_float(V52), ?list_double(V53), ?list_string(V54), ?list_record(V55)]; +encodeIol(person1, {_, V1, V2, V3, V4}) -> + [<<1001:16/big-unsigned>>, ?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; +encodeIol(_, _) -> + []. + +decodeRec(1, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + MsgRec = {test, V1}, + {MsgRec, LeftBin1}; +decodeRec(2, LeftBin0) -> + <> = LeftBin0, + case IsUndef1 of + 0 -> + V1 = undefined, + LeftBin2 = LeftBin1 ; + _ -> + {V1, LeftBin2} = decodeRec(1, LeftBin1) + end, + <> = LeftBin2, + MsgRec = {phoneNumber, V1, V2}, + {MsgRec, LeftBin3}; +decodeRec(3, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + <> = LeftBin2, + case Len2 < ?BinaryShareSize of + true -> + V3 = TemStrV3; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V3 = binary:copy(TemStrV3); + _ -> + V3 = TemStrV3 + end + end, + <> = LeftBin3, + {V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []), + MsgRec = {person, V1, V2, V3, V4}, + {MsgRec, LeftBin5}; +decodeRec(5, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + MsgRec = {union, V1, V2}, + {MsgRec, LeftBin2}; +decodeRec(_, _) -> + {{}, <<>>}. + +decodeBin(1, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + {test, V1}; +decodeBin(2, LeftBin0) -> + <> = LeftBin0, + case IsUndef1 of + 0 -> + V1 = undefined, + LeftBin2 = LeftBin1 ; + _ -> + {V1, LeftBin2} = decodeRec(1, LeftBin1) + end, + <> = LeftBin2, + {phoneNumber, V1, V2}; +decodeBin(3, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + <> = LeftBin2, + case Len2 < ?BinaryShareSize of + true -> + V3 = TemStrV3; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V3 = binary:copy(TemStrV3); + _ -> + V3 = TemStrV3 + end + end, + <> = LeftBin3, + {V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []), + {person, V1, V2, V3, V4}; +decodeBin(4, LeftBin0) -> + <> = LeftBin0, + {V1, LeftBin2} = deRecordList(Len1, 3, LeftBin1, []), + <> = LeftBin2, + {V2, LeftBin4} = deRecordList(Len2, 3, LeftBin3, []), + {addressBook, V1, V2}; +decodeBin(5, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + {union, V1, V2}; +decodeBin(6, LeftBin0) -> + <> = LeftBin0, + V1 = Bool1 =:= 1, + {tbool, V1}; +decodeBin(7, LeftBin0) -> + <> = LeftBin0, + {tint8, V1, V2}; +decodeBin(8, LeftBin0) -> + <> = LeftBin0, + {tuint8, V1, V2}; +decodeBin(9, LeftBin0) -> + <> = LeftBin0, + {tint16, V1, V2}; +decodeBin(10, LeftBin0) -> + <> = LeftBin0, + {tuint16, V1, V2}; +decodeBin(11, LeftBin0) -> + <> = LeftBin0, + {tint32, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}; +decodeBin(12, LeftBin0) -> + <> = LeftBin0, + {tuint32, V1, V2}; +decodeBin(13, LeftBin0) -> + <> = LeftBin0, + {tint64, V1, V2}; +decodeBin(14, LeftBin0) -> + <> = LeftBin0, + {tuint64, V1, V2}; +decodeBin(15, LeftBin0) -> + <> = LeftBin0, + {tinteger, V1, V2, V3, V4, V5, V6, V7, V8}; +decodeBin(16, LeftBin0) -> + <> = LeftBin0, + case NumBits1 of + 33-> + <> = LeftBin1; + 65 -> + <> = LeftBin1; + _ -> + <> = LeftBin1 + end, + <> = LeftBin2, + case NumBits2 of + 33-> + <> = LeftBin3; + 65 -> + <> = LeftBin3; + _ -> + <> = LeftBin3 + end, + <> = LeftBin4, + case NumBits3 of + 33-> + <> = LeftBin5; + 65 -> + <> = LeftBin5; + _ -> + <> = LeftBin5 + end, + <> = LeftBin6, + case NumBits4 of + 33-> + <> = LeftBin7; + 65 -> + <> = LeftBin7; + _ -> + <> = LeftBin7 + end, + <> = LeftBin8, + case NumBits5 of + 33-> + <> = LeftBin9; + 65 -> + <> = LeftBin9; + _ -> + <> = LeftBin9 + end, + <> = LeftBin10, + case NumBits6 of + 33-> + <> = LeftBin11; + 65 -> + <> = LeftBin11; + _ -> + <> = LeftBin11 + end, + <> = LeftBin12, + case NumBits7 of + 33-> + <> = LeftBin13; + 65 -> + <> = LeftBin13; + _ -> + <> = LeftBin13 + end, + <> = LeftBin14, + case NumBits8 of + 33-> + <> = LeftBin15; + 65 -> + <> = LeftBin15; + _ -> + <> = LeftBin15 + end, + <> = LeftBin16, + case NumBits9 of + 33-> + <> = LeftBin17; + 65 -> + <> = LeftBin17; + _ -> + <> = LeftBin17 + end, + <> = LeftBin18, + case NumBits10 of + 33-> + <> = LeftBin19; + 65 -> + <> = LeftBin19; + _ -> + <> = LeftBin19 + end, + {tnumber, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}; +decodeBin(17, LeftBin0) -> + <> = LeftBin0, + {tfloat, V1, V2}; +decodeBin(18, LeftBin0) -> + <> = LeftBin0, + {tdouble, V1, V2}; +decodeBin(19, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + case Len2 < ?BinaryShareSize of + true -> + V2 = TemStrV2; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V2 = binary:copy(TemStrV2); + _ -> + V2 = TemStrV2 + end + end, + {tstring, V1, V2}; +decodeBin(20, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV =:= 1 || <> <= ListBin1], + {tlistbool, V1}; +decodeBin(21, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistint8, V1}; +decodeBin(22, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistuint8, V1}; +decodeBin(23, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistint16, V1}; +decodeBin(24, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistuint16, V1}; +decodeBin(25, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistint32, V1}; +decodeBin(26, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistuint32, V1}; +decodeBin(27, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistint64, V1}; +decodeBin(28, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistuint64, V1}; +decodeBin(29, LeftBin0) -> + <> = LeftBin0, + {V1, LeftBin2} = deIntegerList(Len1, LeftBin1, []), + {tlistinteger, V1}; +decodeBin(30, LeftBin0) -> + <> = LeftBin0, + {V1, LeftBin2} = deNumberList(Len1, LeftBin1, []), + {tlistnumber, V1}; +decodeBin(31, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistfloat, V1}; +decodeBin(32, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistdouble, V1}; +decodeBin(33, LeftBin0) -> + <> = LeftBin0, + RefSize = binary:referenced_byte_size(LeftBin0), + {V1, LeftBin2} = deStringList(Len1, LeftBin1, RefSize, []), + {tliststring, V1}; +decodeBin(34, LeftBin0) -> + <> = LeftBin0, + {V1, LeftBin2} = deRecordList(Len1, 5, LeftBin1, []), + {tlistunion, V1}; +decodeBin(35, LeftBin0) -> + <> = LeftBin0, + V1 = Bool1 =:= 1, + <> = LeftBin1, + <> = LeftBin2, + case NumBits1 of + 33-> + <> = LeftBin3; + 65 -> + <> = LeftBin3; + _ -> + <> = LeftBin3 + end, + <> = LeftBin4, + case NumBits2 of + 33-> + <> = LeftBin5; + 65 -> + <> = LeftBin5; + _ -> + <> = LeftBin5 + end, + <> = LeftBin6, + case NumBits3 of + 33-> + <> = LeftBin7; + 65 -> + <> = LeftBin7; + _ -> + <> = LeftBin7 + end, + <> = LeftBin8, + case NumBits4 of + 33-> + <> = LeftBin9; + 65 -> + <> = LeftBin9; + _ -> + <> = LeftBin9 + end, + <> = LeftBin10, + case NumBits5 of + 33-> + <> = LeftBin11; + 65 -> + <> = LeftBin11; + _ -> + <> = LeftBin11 + end, + <> = LeftBin12, + case NumBits6 of + 33-> + <> = LeftBin13; + 65 -> + <> = LeftBin13; + _ -> + <> = LeftBin13 + end, + <> = LeftBin14, + case NumBits7 of + 33-> + <> = LeftBin15; + 65 -> + <> = LeftBin15; + _ -> + <> = LeftBin15 + end, + <> = LeftBin16, + case NumBits8 of + 33-> + <> = LeftBin17; + 65 -> + <> = LeftBin17; + _ -> + <> = LeftBin17 + end, + <> = LeftBin18, + case NumBits9 of + 33-> + <> = LeftBin19; + 65 -> + <> = LeftBin19; + _ -> + <> = LeftBin19 + end, + <> = LeftBin20, + case NumBits10 of + 33-> + <> = LeftBin21; + 65 -> + <> = LeftBin21; + _ -> + <> = LeftBin21 + end, + <> = LeftBin22, + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin23, + case Len1 < ?BinaryShareSize of + true -> + V30 = TemStrV30; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V30 = binary:copy(TemStrV30); + _ -> + V30 = TemStrV30 + end + end, + <> = LeftBin24, + case Len2 < ?BinaryShareSize of + true -> + V31 = TemStrV31; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V31 = binary:copy(TemStrV31); + _ -> + V31 = TemStrV31 + end + end, + <> = LeftBin25, + case IsUndef1 of + 0 -> + V32 = undefined, + LeftBin27 = LeftBin26 ; + _ -> + {V32, LeftBin27} = decodeRec(5, LeftBin26) + end, + <> = LeftBin27, + <> = LeftBin28, + V33 = [TemV =:= 1 || <> <= ListBin1], + <> = LeftBin29, + <> = LeftBin30, + V34 = [TemV || <> <= ListBin2], + <> = LeftBin31, + <> = LeftBin32, + V35 = [TemV || <> <= ListBin3], + <> = LeftBin33, + <> = LeftBin34, + V36 = [TemV || <> <= ListBin4], + <> = LeftBin35, + <> = LeftBin36, + V37 = [TemV || <> <= ListBin5], + <> = LeftBin37, + <> = LeftBin38, + V38 = [TemV || <> <= ListBin6], + <> = LeftBin39, + <> = LeftBin40, + V39 = [TemV || <> <= ListBin7], + <> = LeftBin41, + <> = LeftBin42, + V40 = [TemV || <> <= ListBin8], + <> = LeftBin43, + <> = LeftBin44, + V41 = [TemV || <> <= ListBin9], + <> = LeftBin45, + {V42, LeftBin47} = deIntegerList(Len12, LeftBin46, []), + <> = LeftBin47, + {V43, LeftBin49} = deIntegerList(Len13, LeftBin48, []), + <> = LeftBin49, + {V44, LeftBin51} = deIntegerList(Len14, LeftBin50, []), + <> = LeftBin51, + {V45, LeftBin53} = deIntegerList(Len15, LeftBin52, []), + <> = LeftBin53, + {V46, LeftBin55} = deNumberList(Len16, LeftBin54, []), + <> = LeftBin55, + {V47, LeftBin57} = deNumberList(Len17, LeftBin56, []), + <> = LeftBin57, + {V48, LeftBin59} = deNumberList(Len18, LeftBin58, []), + <> = LeftBin59, + {V49, LeftBin61} = deNumberList(Len19, LeftBin60, []), + <> = LeftBin61, + {V50, LeftBin63} = deNumberList(Len20, LeftBin62, []), + <> = LeftBin63, + {V51, LeftBin65} = deNumberList(Len21, LeftBin64, []), + <> = LeftBin65, + <> = LeftBin66, + V52 = [TemV || <> <= ListBin20], + <> = LeftBin67, + <> = LeftBin68, + V53 = [TemV || <> <= ListBin21], + <> = LeftBin69, + {V54, LeftBin71} = deStringList(Len24, LeftBin70, RefSize, []), + <> = LeftBin71, + {V55, LeftBin73} = deRecordList(Len25, 5, LeftBin72, []), + {allType, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}; +decodeBin(1001, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + <> = LeftBin2, + case Len2 < ?BinaryShareSize of + true -> + V3 = TemStrV3; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V3 = binary:copy(TemStrV3); + _ -> + V3 = TemStrV3 + end + end, + <> = LeftBin3, + {V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []), + {person1, V1, V2, V3, V4}; +decodeBin(_, _) -> + {{}, <<>>}. + diff --git a/test/V2/protoMsg.hrl b/test/V2/protoMsg.hrl new file mode 100644 index 0000000..23f2743 --- /dev/null +++ b/test/V2/protoMsg.hrl @@ -0,0 +1,226 @@ +-opaque int8() :: -128..127. +-opaque int16() :: -32768..32767. +-opaque int32() :: -2147483648..2147483647. +-opaque int64() :: -9223372036854775808..9223372036854775807. +-opaque uint8() :: 0..255. +-opaque uint16() :: 0..65536. +-opaque uint32() :: 0..4294967295. +-opaque uint64() :: 0..18446744073709551615. +-opaque double() :: float(). + +-define(ERR1, 1). %% 辅导费 +-define(ERR2, 2). %% 444 +-define(ERR3, 3). %% 辅导费 +-define(ERR4, 4). %% dfsf +-define(ERR5, 5). %% 其他注释辅导费 err6:dfff +-define(ERR7, 6). %% def +-define(ERR8, 7). %% 其他注释辅导费 +-define(ERR6, 1001). %% dfff + + +-record(test, { + aa = "" :: string() +}). +-record(phoneNumber, { + number = undefined :: #test{} + , type = 0 :: int32() +}). +-record(person, { + name = "" :: string() + , id = 0 :: int32() + , email = "" :: string() + , phone = [] :: [#phoneNumber{}] +}). +-record(addressBook, { + person = [] :: [#person{}] + , other = [] :: [#person{}] +}). +-record(union, { + test = "" :: string() + , type = 0 :: int32() +}). +-record(tbool, { + bool = false :: boolean() +}). +-record(tint8, { + int1 = 0 :: int8() + , int2 = 0 :: int8() +}). +-record(tuint8, { + int1 = 0 :: uint8() + , int2 = 0 :: uint8() +}). +-record(tint16, { + int1 = 0 :: int16() + , int2 = 0 :: int16() +}). +-record(tuint16, { + int1 = 0 :: uint16() + , int2 = 0 :: uint16() +}). +-record(tint32, { + int1 = 0 :: int32() + , int2 = 0 :: int32() + , int3 = 0 :: int32() + , int4 = 0 :: int32() + , int5 = 0 :: int32() + , int6 = 0 :: int32() + , int7 = 0 :: int32() + , int8 = 0 :: int32() + , int9 = 0 :: int32() + , int10 = 0 :: int32() +}). +-record(tuint32, { + int1 = 0 :: uint32() + , int2 = 0 :: uint32() +}). +-record(tint64, { + int1 = 0 :: int64() + , int2 = 0 :: int64() +}). +-record(tuint64, { + int1 = 0 :: uint64() + , int2 = 0 :: uint64() +}). +-record(tinteger, { + int1 = 0 :: integer() + , int2 = 0 :: integer() + , int3 = 0 :: integer() + , int4 = 0 :: integer() + , int5 = 0 :: integer() + , int6 = 0 :: integer() + , int7 = 0 :: integer() + , int8 = 0 :: integer() +}). +-record(tnumber, { + int1 = 0 :: number() + , int2 = 0 :: number() + , int3 = 0 :: number() + , int4 = 0 :: number() + , int5 = 0 :: number() + , int6 = 0 :: number() + , int7 = 0 :: number() + , int8 = 0 :: number() + , float1 = 0 :: number() + , float2 = 0 :: number() +}). +-record(tfloat, { + int1 = 0.0 :: float() + , int2 = 0.0 :: float() +}). +-record(tdouble, { + int1 = 0.0 :: double() + , int2 = 0.0 :: double() +}). +-record(tstring, { + int1 = "" :: string() + , int2 = "" :: string() +}). +-record(tlistbool, { + int1 = [] :: [boolean()] +}). +-record(tlistint8, { + int1 = [] :: [int8()] +}). +-record(tlistuint8, { + int1 = [] :: [uint8()] +}). +-record(tlistint16, { + int1 = [] :: [int16()] +}). +-record(tlistuint16, { + int1 = [] :: [uint16()] +}). +-record(tlistint32, { + int1 = [] :: [int32()] +}). +-record(tlistuint32, { + int1 = [] :: [uint32()] +}). +-record(tlistint64, { + int1 = [] :: [int64()] +}). +-record(tlistuint64, { + int1 = [] :: [uint64()] +}). +-record(tlistinteger, { + int1 = [] :: [integer()] +}). +-record(tlistnumber, { + int1 = [] :: [number()] +}). +-record(tlistfloat, { + int1 = [] :: [float()] +}). +-record(tlistdouble, { + int1 = [] :: [double()] +}). +-record(tliststring, { + int1 = [] :: [string()] +}). +-record(tlistunion, { + int1 = [] :: [#union{}] +}). +-record(allType, { + bool = false :: boolean() + , int8 = 0 :: int8() + , uint8 = 0 :: uint8() + , int16 = 0 :: int16() + , uint16 = 0 :: uint16() + , int32 = 0 :: int32() + , uint32 = 0 :: uint32() + , int64 = 0 :: int64() + , uint64 = 0 :: uint64() + , inte8 = 0 :: integer() + , uinte8 = 0 :: integer() + , inte16 = 0 :: integer() + , uinte16 = 0 :: integer() + , inte32 = 0 :: integer() + , uinte32 = 0 :: integer() + , inte64 = 0 :: integer() + , uinte64 = 0 :: integer() + , num8 = 0 :: number() + , unum8 = 0 :: number() + , num16 = 0 :: number() + , unum16 = 0 :: number() + , num32 = 0 :: number() + , unum32 = 0 :: number() + , num64 = 0 :: number() + , unum64 = 0 :: number() + , numfloat = 0 :: number() + , numdouble = 0 :: number() + , float = 0.0 :: float() + , double = 0.0 :: double() + , string1 = "" :: string() + , string2 = "" :: string() + , union = undefined :: #union{} + , lbool = [] :: [boolean()] + , lint8 = [] :: [int8()] + , luint8 = [] :: [uint8()] + , lint16 = [] :: [int16()] + , luint16 = [] :: [uint16()] + , lint32 = [] :: [int32()] + , luint32 = [] :: [uint32()] + , lint64 = [] :: [int64()] + , luint64 = [] :: [uint64()] + , linte8 = [] :: [integer()] + , linte16 = [] :: [integer()] + , linte32 = [] :: [integer()] + , linte64 = [] :: [integer()] + , lnum8 = [] :: [number()] + , lnum16 = [] :: [number()] + , lnum32 = [] :: [number()] + , lnum64 = [] :: [number()] + , lnfloat32 = [] :: [number()] + , lnfloat64 = [] :: [number()] + , lfloat = [] :: [float()] + , ldouble = [] :: [double()] + , lstring = [] :: [string()] + , lunion = [] :: [#union{}] +}). +-record(person1, { + name = "" :: string() + , id = 0 :: int32() + , email = "" :: string() + , phone = [] :: [#phoneNumber{}] +}). diff --git a/test/V2/test2.erl b/test/V2/test2.erl new file mode 100644 index 0000000..76b66bf --- /dev/null +++ b/test/V2/test2.erl @@ -0,0 +1,672 @@ +-module(test2). + +-include("protoMsg.hrl"). +-compile(export_all). + +encode_int32(N) -> + TT = #tint32{int1 = 1, int2 = -1, int3 = 128, int4 = -128, int5 = 65536, + int6 = -65536, int7 = 2100000000, int8 = -2100000000, int9 = 678665, int10 = -678665}, + tt1(N, TT). + +tt1(0, TT) -> + ok; +tt1(N, TT) -> + protoMsg:encodeIol(TT), + tt1(N - 1, TT). + +decode_int32(N) -> + TT = #tint32{int1 = 1, int2 = -1, int3 = 128, int4 = -128, int5 = 65536, + int6 = -65536, int7 = 2100000000, int8 = -2100000000, int9 = 678665, int10 = -678665}, + Bin = protoMsg:encodeIol(TT), + tt2(N, iolist_to_binary(Bin)). + +tt2(0, Bin) -> + {protoMsg:decode(Bin), Bin}; +tt2(N, Bin) -> + protoMsg:decode(Bin), + tt2(N - 1, Bin). + +encode_addressBook(N) -> + Add = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + tt3(N, Add). + +tt3(0, Add) -> + ok; +tt3(N, Add) -> + protoMsg:encodeIol(Add), + tt3(N - 1, Add). + + +decode_addressBook(N) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + Bin = protoMsg:encodeIol(AddressBook), + tt4(N, iolist_to_binary(Bin)). +tt4(0, Bin) -> + Bin; +tt4(N, Bin) -> + protoMsg:decode(Bin), + tt4(N - 1, Bin). + +test1() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tbool{bool = true}))). + +test21() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tint8{int1 = 123, int2 = -22}))). + +test22() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tuint8{int1 = 123, int2 = 182}))). + +test31() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tint16{int1 = 12343, int2 = -3422}))). +test32() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tuint16{int1 = 43244, int2 = 43243}))). + +test41() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tint32{int1 = 12343434, int2 = -34434322}))). + + +test42() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tuint32{int1 = 432444343, int2 = 432443433}))). + +test51() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tint64{int1 = 12344343434, int2 = -344343434322}))). +test52() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tuint64{int1 = 4343432444343, int2 = 4324434343433}))). + +tt6(N) -> + Bin = iolist_to_binary(protoMsg:encodeIol(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434})), + <<_MsgId:16/big, MsgBin/binary>> = Bin, + test6(N, MsgBin). + +test6(0, Bin) -> + io:format("IMY******111 ~p~n", [protoMsg:decode(Bin)]); +test6(N, Bin) -> + protoMsg:decodeBin(15, Bin), + test6(N - 1, Bin). + +tt66(N) -> + Bin = iolist_to_binary(protoMsg:encodeIol(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434})), + test66(N, Bin). + +test66(0, Bin) -> + <<_MsgId:16/big, MsgBin/binary>> = Bin, + <> = MsgBin, + A = {tinteger, Int1, Int2, Int3, Int4, Int5, Int6, Int7, Int8}, + io:format("IMY******111 ~p~n", [A]); +test66(N, Bin) -> + <<_MsgId:16/big, MsgBin/binary>> = Bin, + %% <> = MsgBin, + %% {tinteger, Int1, Int2, Int3, Int4, Int5, Int6, Int7, Int8}, + <> = MsgBin, + {tinteger, V1, V2, V3, V4, V5, V6, V7, V8}, + test66(N - 1, Bin). + +tt67(N) -> + Bin = iolist_to_binary(protoMsg:encodeIol(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434})), + test67(N, Bin). + +test67(0, Bin) -> + A = protoMsg:decode(Bin), + io:format("IMY******111 ~p~n", [A]); +test67(N, Bin) -> + _A = protoMsg:decode(Bin), + test67(N - 1, Bin). + +test7() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tnumber{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434, float1 = -34234343.343, float2 = 43242342342342.434}))). + +test81() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tfloat{int1 = -34234343.343, int2 = 42342342.434}))). +test82() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tdouble{int1 = -342343433333.343, int2 = 423423423333.434}))). + +test9() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tstring{int1 = "dfdf143242", int2 = "发地方撒发送"}))). + +allType() -> + AllType = #allType{ + bool = false + , int8 = -66 + , uint8 = 66 + , int16 = -666 + , uint16 = 666 + , int32 = -66666 + , uint32 = 66666 + , int64 = -5294967296 + , uint64 = 5294967296 + , inte8 = -88 + , uinte8 = 88 + , inte16 = -8888 + , uinte16 = 8888 + , inte32 = -888888 + , uinte32 = 888888 + , inte64 = -8888888888 + , uinte64 = 8888888888 + , num8 = -99 + , unum8 = 99 + , num16 = -9999 + , unum16 = 9999 + , num32 = -999999 + , unum32 = 999999 + , num64 = -9999999999 + , unum64 = 9999999999 + , numfloat = 999999.99999 + , numdouble = 9999999999.99999 + , float = 123456.789321 + , double = 4300000000.789321 + , string1 = "this is a test!!!!" + , string2 = "这是一个测试, 等会看结果~!!!" + , lbool = [true, false, true, false] + , lint8 = [123, -123, 66, -66, 88, -88] + , luint8 = [1, 2, 3, 123, 67, 88] + , lint16 = [-12345, 12345, 6666, -6666] + , luint16 = [12345, 12345, 6666, 6666] + , lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666] + , luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666] + , lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666] + , luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666] + , linte8 = [123, 12, -66, 66, 34] + , linte16 = [12334, 12, -6666, 6666, 3412] + , linte32 = [12334, 12, -666666666, 6666, 3412] + , linte64 = [12334, 12, -666666666666, 6666, 3412] + , lnum8 = [123, 12.123, -66.456789, 66, 34] + , lnum16 = [12334, -12, -6666.6666, 6666, 3412] + , lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412] + , lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999] + , lnfloat32 = [-666666.88888, 4434.434, 434.43, 3434] + , lnfloat64 = [-666666666666.88888, 4434.434, 434.43, 11111111111.34343, 5566] + , lfloat = [1.1, 2.2, 3.3, 666666.666] + , ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666] + , lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式"/utf8>>] + , lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}] + }, + List = protoMsg:encodeIol(AllType), + iolist_to_binary(List), + %%io:format("~p~n", [List]), + AllType1 = protoMsg:decode(iolist_to_binary(List)). + %%AllType1. + +tall1(0) -> + ok; +tall1(N) -> + AllType = #allType{ + bool = false + , int8 = -66 + , uint8 = 66 + , int16 = -666 + , uint16 = 666 + , int32 = -66666 + , uint32 = 66666 + , int64 = -5294967296 + , uint64 = 5294967296 + , inte8 = -88 + , uinte8 = 88 + , inte16 = -8888 + , uinte16 = 8888 + , inte32 = -888888 + , uinte32 = 888888 + , inte64 = -8888888888 + , uinte64 = 8888888888 + , num8 = -99 + , unum8 = 99 + , num16 = -9999 + , unum16 = 9999 + , num32 = -999999 + , unum32 = 999999 + , num64 = -9999999999 + , unum64 = 9999999999 + , numfloat = 999999.99999 + , numdouble = 9999999999.99999 + , float = 123456.789321 + , double = 4300000000.789321 + , string1 = "this is a test!!!!" + , string2 = "这是一个测试, 等会看结果~!!!" + , lbool = [true, false, true, false] + , lint8 = [123, -123, 66, -66, 88, -88] + , luint8 = [1, 2, 3, 123, 67, 88] + , lint16 = [-12345, 12345, 6666, -6666] + , luint16 = [12345, 12345, 6666, 6666] + , lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666] + , luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666] + , lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666] + , luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666] + , linte8 = [123, 12, -66, 66, 34] + , linte16 = [12334, 12, -6666, 6666, 3412] + , linte32 = [12334, 12, -666666666, 6666, 3412] + , linte64 = [12334, 12, -666666666666, 6666, 3412] + , lnum8 = [123, 12.123, -66.456789, 66, 34] + , lnum16 = [12334, -12, -6666.6666, 6666, 3412] + , lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412] + , lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999] + , lnfloat32 = [-666666.88888, 4434.434, 434.43, 3434] + , lnfloat64 = [-666666666666.88888, 4434.434, 434.43, 11111111111.34343, 5566] + , lfloat = [1.1, 2.2, 3.3, 666666.666] + , ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666] + , lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式"/utf8>>] + , lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}] + }, + %protoMsg:encodeIol(AllType), + term_to_binary(AllType), + tall1(N - 1). + + +tall(N) -> + AllType = #allType{ + bool = false + , int8 = -66 + , uint8 = 66 + , int16 = -666 + , uint16 = 666 + , int32 = -66666 + , uint32 = 66666 + , int64 = -5294967296 + , uint64 = 5294967296 + , inte8 = -88 + , uinte8 = 88 + , inte16 = -8888 + , uinte16 = 8888 + , inte32 = -888888 + , uinte32 = 888888 + , inte64 = -8888888888 + , uinte64 = 8888888888 + , num8 = -99 + , unum8 = 99 + , num16 = -9999 + , unum16 = 9999 + , num32 = -999999 + , unum32 = 999999 + , num64 = -9999999999 + , unum64 = 9999999999 + , numfloat = 999999.99999 + , numdouble = 9999999999.99999 + , float = 123456.789321 + , double = 4300000000.789321 + , string1 = "this is a test!!!!" + , string2 = "这是一个测试, 等会看结果~!!!" + , lbool = [true, false, true, false] + , lint8 = [123, -123, 66, -66, 88, -88] + , luint8 = [1, 2, 3, 123, 67, 88] + , lint16 = [-12345, 12345, 6666, -6666] + , luint16 = [12345, 12345, 6666, 6666] + , lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666] + , luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666] + , lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666] + , luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666] + , linte8 = [123, 12, -66, 66, 34] + , linte16 = [12334, 12, -6666, 6666, 3412] + , linte32 = [12334, 12, -666666666, 6666, 3412] + , linte64 = [12334, 12, -666666666666, 6666, 3412] + , lnum8 = [123, 12.123, -66.456789, 66, 34] + , lnum16 = [12334, -12, -6666.6666, 6666, 3412] + , lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412] + , lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999] + , lnfloat32 = [-666666.88888, 4434.434, 434.43, 3434] + , lnfloat64 = [-666666666666.88888, 4434.434, 434.43, 11111111111.34343, 5566] + , lfloat = [1.1, 2.2, 3.3, 666666.666] + , ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666] + , lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式"/utf8>>] + , lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}] + }, + %List = protoMsg:encodeIol(AllType), + tall(N, term_to_binary(AllType)). + +tall(0, Bin) -> + Bin; +tall(N, Bin) -> + %protoMsg:decode(Bin), + binary_to_term(Bin), + tall(N - 1, Bin). + + +hh1(0) -> + ok; +hh1(N) -> + A = [tt1, tt2, tt3, tt4, {tet, tt1}, {tet, tt2}, {tet, tt3}, yyy], + test(A), + hh1(N - 1). + +hh2(0) -> + ok; +hh2(N) -> + A = [tt1, tt2, tt3, tt4, {tet, tt1}, {tet, tt2}, {tet, tt3}, yyy], + tet(A), + hh2(N - 1). + +test([]) -> + ok; +test([A | T]) -> + case A of + tt1 -> + ok; + tt2 -> + ok; + tt3 -> + ok; + tt4 -> + ok; + {tet, tt1} -> + ok; + {tet, tt2} -> + ok; + {tet, tt3} -> + ok; + _ -> + ok + end, + test(T). + + +tet([]) -> + ok; +tet([tt1 | T]) -> + ok, + tet(T); +tet([tt2 | T]) -> + ok, + tet(T); +tet([tt3 | T]) -> + ok, + tet(T); +tet([tt4 | T]) -> + ok, + tet(T); +tet([{tet, tt1} | T]) -> + ok, + tet(T); +tet([{tet, tt2} | T]) -> + ok, + tet(T); +tet([{tet, tt3} | T]) -> + ok, + tet(T); +tet([YY | T]) -> + ok, + tet(T). + +ttt11(N) -> + Add = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + ttt11(N, Add). + +ttt11(0, Add) -> + ok; +ttt11(N, Add) -> + protoMsg:encodeIol(Add), + ttt11(N - 1, Add). + +%%tt1(Add) -> + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + %%protoMsg:encodeIol(Add). +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%Bin = protoCode:encode(AddressBook), +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%MsgId = protoMsg:getMsgId(element(1, AddressBook)), +%<>. + + +tt(N) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + Bin = protoMsg:encodeIol(AddressBook), + %ok = file:write_file("fff.bin", Bin), + %print_bin(Bin), + tt(N, iolist_to_binary(Bin)). +tt(0, Bin) -> + protoMsg:decode(Bin); +tt(N, Bin) -> + protoMsg:decode(Bin), + tt(N - 1, Bin). + + +%{Len, List, RestBin} = protoMsg("AddressBook", Bin), +%io:format("Len:~p, RestBin:~p~n", [Len, RestBin]), +%io:format("List:~p~n", [List]), +%{Map, _, _} = sproto:decode2("AddressBook", Bin), +%Map. + + +ttt1(0) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "你好啊 嘿嘿"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "范德萨地方范德萨发"}, type = 3} + ] + } + ] + }, + term_to_binary(AddressBook); +ttt1(N) -> + ttt1(), + ttt1(N - 1). + +ttt1() -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "你好啊 嘿嘿"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "范德萨地方范德萨发"}, type = 3} + ] + } + ] + }, + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + Bin = term_to_binary(AddressBook). +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%Bin = protoCode:encode(AddressBook), +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%MsgId = protoMsg:getMsgId(element(1, AddressBook)), +%<>. + + +ttt(N) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + %ok = file:write_file("fff.bin", Bin), + %print_bin(Bin), + ttt(N, term_to_binary(AddressBook)). +ttt(0, Bin) -> + binary_to_term(Bin); +ttt(N, Bin) -> + binary_to_term(Bin), + ttt(N - 1, Bin). + +print_bin(Bin) -> + ByteList = lists:reverse(bin_to_hex(Bin, [])), + Fun = fun(Byte, Acc) -> + io:format("~2.16.0b ", [Byte]), + case Acc rem 8 =:= 0 of + true -> io:format("~n", []); + false -> ok + end, + Acc + 1 + end, + lists:foldl(Fun, 1, ByteList), + io:format("bytes:~p~n", [byte_size(Bin)]). + +bin_to_hex(<<>>, Acc) -> + Acc; +bin_to_hex(Bin, Acc) -> + <> = Bin, + bin_to_hex(Bin2, [A | Acc]). + +a1(B) -> + Bin = list_to_binary([X rem 256 || X <- lists:seq(1, 10000)]), + a1(B, Bin). + +a1(0, Bin) -> + <> = Bin, + [X || <> <= ListBin]; +a1(N, Bin) -> + <> = Bin, + A = [X || <> <= ListBin], + B = [X || <> <= ListBin], + io:format("IMY********** ~p~n", [A == B]), + a1(N - 1, Bin). + +a2(B) -> + Bin = list_to_binary([X rem 256 || X <- lists:seq(1, 10000)]), + a2(B, Bin). + +a2(0, Bin) -> + Len = 200, + <> = Bin, + [X || <> <= ListBin]; +a2(N, Bin) -> + Len = 200, + <> = Bin, + [X || <> <= ListBin], + a2(N - 1, Bin). \ No newline at end of file diff --git a/test/protoMsg.erl b/test/protoMsg.erl index 630dbb9..1ddf14f 100644 --- a/test/protoMsg.erl +++ b/test/protoMsg.erl @@ -3,7 +3,7 @@ -compile([nowarn_unused_vars]). --export([encode/1, decode/1, encodeRec/1, decodeBin/2]). +-export([encodeIol/1, encodeBin/1, encodeIol/2, subEncode/1, subEncode/2, decode/1, decodeBin/2]). -define(min8, -128). -define(max8, 127). @@ -33,7 +33,7 @@ -define(float(V), <>). -define(double(V), <>). -define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)). --define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)). +-define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, subEncode(V)] end)). -define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]). -define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]). -define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]). @@ -48,7 +48,7 @@ -define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]). -define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]). -define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]). --define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]). +-define(list_record(List), [<<(length(List)):16/big>>, [subEncode(V) || V <- List]]). -define(BinaryShareSize, 65). -define(BinaryCopyRatio, 1.2). @@ -150,90 +150,99 @@ deRecordList(N, MsgId, MsgBin, RetList) -> {Tuple, LeftBin} = decodeRec(MsgId, MsgBin), deRecordList(N - 1, MsgId, LeftBin, [Tuple | RetList]). -encodeRec({test, V1}) -> +encodeIol(RecMsg) -> + encodeIol(erlang:element(1, RecMsg), RecMsg). + +encodeBin(RecMsg) -> + erlang:iolist_to_binary(encodeIol(RecMsg)). + +subEncode(RecMsg) -> + subEncode(erlang:element(1, RecMsg), RecMsg). + +subEncode(test, {_, V1}) -> [?string(V1)]; -encodeRec({phoneNumber, V1, V2}) -> +subEncode(phoneNumber, {_, V1, V2}) -> [?record(V1), ?int32(V2)]; -encodeRec({person, V1, V2, V3, V4}) -> +subEncode(person, {_, V1, V2, V3, V4}) -> [?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; -encodeRec({union, V1, V2}) -> +subEncode(union, {_, V1, V2}) -> [?string(V1), ?int32(V2)]; -encodeRec(_) -> +subEncode(_, _) -> []. -encode({test, V1}) -> +encodeIol(test, {_, V1}) -> [<<1:16/big-unsigned>>, ?string(V1)]; -encode({phoneNumber, V1, V2}) -> +encodeIol(phoneNumber, {_, V1, V2}) -> [<<2:16/big-unsigned>>, ?record(V1), ?int32(V2)]; -encode({person, V1, V2, V3, V4}) -> +encodeIol(person, {_, V1, V2, V3, V4}) -> [<<3:16/big-unsigned>>, ?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; -encode({addressBook, V1, V2}) -> +encodeIol(addressBook, {_, V1, V2}) -> [<<4:16/big-unsigned>>, ?list_record(V1), ?list_record(V2)]; -encode({union, V1, V2}) -> +encodeIol(union, {_, V1, V2}) -> [<<5:16/big-unsigned>>, ?string(V1), ?int32(V2)]; -encode({tbool, V1}) -> +encodeIol(tbool, {_, V1}) -> [<<6:16/big-unsigned>>, ?bool(V1)]; -encode({tint8, V1, V2}) -> +encodeIol(tint8, {_, V1, V2}) -> [<<7:16/big-unsigned>>, ?int8(V1), ?int8(V2)]; -encode({tuint8, V1, V2}) -> +encodeIol(tuint8, {_, V1, V2}) -> [<<8:16/big-unsigned>>, ?uint8(V1), ?uint8(V2)]; -encode({tint16, V1, V2}) -> +encodeIol(tint16, {_, V1, V2}) -> [<<9:16/big-unsigned>>, ?int16(V1), ?int16(V2)]; -encode({tuint16, V1, V2}) -> +encodeIol(tuint16, {_, V1, V2}) -> [<<10:16/big-unsigned>>, ?uint16(V1), ?uint16(V2)]; -encode({tint32, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) -> +encodeIol(tint32, {_, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) -> [<<11:16/big-unsigned>>, ?int32(V1), ?int32(V2), ?int32(V3), ?int32(V4), ?int32(V5), ?int32(V6), ?int32(V7), ?int32(V8), ?int32(V9), ?int32(V10)]; -encode({tuint32, V1, V2}) -> +encodeIol(tuint32, {_, V1, V2}) -> [<<12:16/big-unsigned>>, ?uint32(V1), ?uint32(V2)]; -encode({tint64, V1, V2}) -> +encodeIol(tint64, {_, V1, V2}) -> [<<13:16/big-unsigned>>, ?int64(V1), ?int64(V2)]; -encode({tuint64, V1, V2}) -> +encodeIol(tuint64, {_, V1, V2}) -> [<<14:16/big-unsigned>>, ?uint64(V1), ?uint64(V2)]; -encode({tinteger, V1, V2, V3, V4, V5, V6, V7, V8}) -> +encodeIol(tinteger, {_, V1, V2, V3, V4, V5, V6, V7, V8}) -> [<<15:16/big-unsigned>>, ?integer(V1), ?integer(V2), ?integer(V3), ?integer(V4), ?integer(V5), ?integer(V6), ?integer(V7), ?integer(V8)]; -encode({tnumber, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) -> +encodeIol(tnumber, {_, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) -> [<<16:16/big-unsigned>>, ?number(V1), ?number(V2), ?number(V3), ?number(V4), ?number(V5), ?number(V6), ?number(V7), ?number(V8), ?number(V9), ?number(V10)]; -encode({tfloat, V1, V2}) -> +encodeIol(tfloat, {_, V1, V2}) -> [<<17:16/big-unsigned>>, ?float(V1), ?float(V2)]; -encode({tdouble, V1, V2}) -> +encodeIol(tdouble, {_, V1, V2}) -> [<<18:16/big-unsigned>>, ?double(V1), ?double(V2)]; -encode({tstring, V1, V2}) -> +encodeIol(tstring, {_, V1, V2}) -> [<<19:16/big-unsigned>>, ?string(V1), ?string(V2)]; -encode({tlistbool, V1}) -> +encodeIol(tlistbool, {_, V1}) -> [<<20:16/big-unsigned>>, ?list_bool(V1)]; -encode({tlistint8, V1}) -> +encodeIol(tlistint8, {_, V1}) -> [<<21:16/big-unsigned>>, ?list_int8(V1)]; -encode({tlistuint8, V1}) -> +encodeIol(tlistuint8, {_, V1}) -> [<<22:16/big-unsigned>>, ?list_uint8(V1)]; -encode({tlistint16, V1}) -> +encodeIol(tlistint16, {_, V1}) -> [<<23:16/big-unsigned>>, ?list_int16(V1)]; -encode({tlistuint16, V1}) -> +encodeIol(tlistuint16, {_, V1}) -> [<<24:16/big-unsigned>>, ?list_uint16(V1)]; -encode({tlistint32, V1}) -> +encodeIol(tlistint32, {_, V1}) -> [<<25:16/big-unsigned>>, ?list_int32(V1)]; -encode({tlistuint32, V1}) -> +encodeIol(tlistuint32, {_, V1}) -> [<<26:16/big-unsigned>>, ?list_uint32(V1)]; -encode({tlistint64, V1}) -> +encodeIol(tlistint64, {_, V1}) -> [<<27:16/big-unsigned>>, ?list_int64(V1)]; -encode({tlistuint64, V1}) -> +encodeIol(tlistuint64, {_, V1}) -> [<<28:16/big-unsigned>>, ?list_uint64(V1)]; -encode({tlistinteger, V1}) -> +encodeIol(tlistinteger, {_, V1}) -> [<<29:16/big-unsigned>>, ?list_integer(V1)]; -encode({tlistnumber, V1}) -> +encodeIol(tlistnumber, {_, V1}) -> [<<30:16/big-unsigned>>, ?list_number(V1)]; -encode({tlistfloat, V1}) -> +encodeIol(tlistfloat, {_, V1}) -> [<<31:16/big-unsigned>>, ?list_float(V1)]; -encode({tlistdouble, V1}) -> +encodeIol(tlistdouble, {_, V1}) -> [<<32:16/big-unsigned>>, ?list_double(V1)]; -encode({tliststring, V1}) -> +encodeIol(tliststring, {_, V1}) -> [<<33:16/big-unsigned>>, ?list_string(V1)]; -encode({tlistunion, V1}) -> +encodeIol(tlistunion, {_, V1}) -> [<<34:16/big-unsigned>>, ?list_record(V1)]; -encode({allType, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}) -> +encodeIol(allType, {_, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}) -> [<<35:16/big-unsigned>>, ?bool(V1), ?int8(V2), ?uint8(V3), ?int16(V4), ?uint16(V5), ?int32(V6), ?uint32(V7), ?int64(V8), ?uint64(V9), ?integer(V10), ?integer(V11), ?integer(V12), ?integer(V13), ?integer(V14), ?integer(V15), ?integer(V16), ?integer(V17), ?number(V18), ?number(V19), ?number(V20), ?number(V21), ?number(V22), ?number(V23), ?number(V24), ?number(V25), ?number(V26), ?number(V27), ?float(V28), ?double(V29), ?string(V30), ?string(V31), ?record(V32), ?list_bool(V33), ?list_int8(V34), ?list_uint8(V35), ?list_int16(V36), ?list_uint16(V37), ?list_int32(V38), ?list_uint32(V39), ?list_int64(V40), ?list_uint64(V41), ?list_integer(V42), ?list_integer(V43), ?list_integer(V44), ?list_integer(V45), ?list_number(V46), ?list_number(V47), ?list_number(V48), ?list_number(V49), ?list_number(V50), ?list_number(V51), ?list_float(V52), ?list_double(V53), ?list_string(V54), ?list_record(V55)]; -encode({person1, V1, V2, V3, V4}) -> +encodeIol(person1, {_, V1, V2, V3, V4}) -> [<<1001:16/big-unsigned>>, ?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; -encode(_) -> +encodeIol(_, _) -> []. decodeRec(1, LeftBin0) -> diff --git a/test/test2.erl b/test/test2.erl new file mode 100644 index 0000000..76b66bf --- /dev/null +++ b/test/test2.erl @@ -0,0 +1,672 @@ +-module(test2). + +-include("protoMsg.hrl"). +-compile(export_all). + +encode_int32(N) -> + TT = #tint32{int1 = 1, int2 = -1, int3 = 128, int4 = -128, int5 = 65536, + int6 = -65536, int7 = 2100000000, int8 = -2100000000, int9 = 678665, int10 = -678665}, + tt1(N, TT). + +tt1(0, TT) -> + ok; +tt1(N, TT) -> + protoMsg:encodeIol(TT), + tt1(N - 1, TT). + +decode_int32(N) -> + TT = #tint32{int1 = 1, int2 = -1, int3 = 128, int4 = -128, int5 = 65536, + int6 = -65536, int7 = 2100000000, int8 = -2100000000, int9 = 678665, int10 = -678665}, + Bin = protoMsg:encodeIol(TT), + tt2(N, iolist_to_binary(Bin)). + +tt2(0, Bin) -> + {protoMsg:decode(Bin), Bin}; +tt2(N, Bin) -> + protoMsg:decode(Bin), + tt2(N - 1, Bin). + +encode_addressBook(N) -> + Add = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + tt3(N, Add). + +tt3(0, Add) -> + ok; +tt3(N, Add) -> + protoMsg:encodeIol(Add), + tt3(N - 1, Add). + + +decode_addressBook(N) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + Bin = protoMsg:encodeIol(AddressBook), + tt4(N, iolist_to_binary(Bin)). +tt4(0, Bin) -> + Bin; +tt4(N, Bin) -> + protoMsg:decode(Bin), + tt4(N - 1, Bin). + +test1() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tbool{bool = true}))). + +test21() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tint8{int1 = 123, int2 = -22}))). + +test22() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tuint8{int1 = 123, int2 = 182}))). + +test31() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tint16{int1 = 12343, int2 = -3422}))). +test32() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tuint16{int1 = 43244, int2 = 43243}))). + +test41() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tint32{int1 = 12343434, int2 = -34434322}))). + + +test42() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tuint32{int1 = 432444343, int2 = 432443433}))). + +test51() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tint64{int1 = 12344343434, int2 = -344343434322}))). +test52() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tuint64{int1 = 4343432444343, int2 = 4324434343433}))). + +tt6(N) -> + Bin = iolist_to_binary(protoMsg:encodeIol(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434})), + <<_MsgId:16/big, MsgBin/binary>> = Bin, + test6(N, MsgBin). + +test6(0, Bin) -> + io:format("IMY******111 ~p~n", [protoMsg:decode(Bin)]); +test6(N, Bin) -> + protoMsg:decodeBin(15, Bin), + test6(N - 1, Bin). + +tt66(N) -> + Bin = iolist_to_binary(protoMsg:encodeIol(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434})), + test66(N, Bin). + +test66(0, Bin) -> + <<_MsgId:16/big, MsgBin/binary>> = Bin, + <> = MsgBin, + A = {tinteger, Int1, Int2, Int3, Int4, Int5, Int6, Int7, Int8}, + io:format("IMY******111 ~p~n", [A]); +test66(N, Bin) -> + <<_MsgId:16/big, MsgBin/binary>> = Bin, + %% <> = MsgBin, + %% {tinteger, Int1, Int2, Int3, Int4, Int5, Int6, Int7, Int8}, + <> = MsgBin, + {tinteger, V1, V2, V3, V4, V5, V6, V7, V8}, + test66(N - 1, Bin). + +tt67(N) -> + Bin = iolist_to_binary(protoMsg:encodeIol(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434})), + test67(N, Bin). + +test67(0, Bin) -> + A = protoMsg:decode(Bin), + io:format("IMY******111 ~p~n", [A]); +test67(N, Bin) -> + _A = protoMsg:decode(Bin), + test67(N - 1, Bin). + +test7() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tnumber{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434, float1 = -34234343.343, float2 = 43242342342342.434}))). + +test81() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tfloat{int1 = -34234343.343, int2 = 42342342.434}))). +test82() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tdouble{int1 = -342343433333.343, int2 = 423423423333.434}))). + +test9() -> + protoMsg:decode(iolist_to_binary(protoMsg:encodeIol(#tstring{int1 = "dfdf143242", int2 = "发地方撒发送"}))). + +allType() -> + AllType = #allType{ + bool = false + , int8 = -66 + , uint8 = 66 + , int16 = -666 + , uint16 = 666 + , int32 = -66666 + , uint32 = 66666 + , int64 = -5294967296 + , uint64 = 5294967296 + , inte8 = -88 + , uinte8 = 88 + , inte16 = -8888 + , uinte16 = 8888 + , inte32 = -888888 + , uinte32 = 888888 + , inte64 = -8888888888 + , uinte64 = 8888888888 + , num8 = -99 + , unum8 = 99 + , num16 = -9999 + , unum16 = 9999 + , num32 = -999999 + , unum32 = 999999 + , num64 = -9999999999 + , unum64 = 9999999999 + , numfloat = 999999.99999 + , numdouble = 9999999999.99999 + , float = 123456.789321 + , double = 4300000000.789321 + , string1 = "this is a test!!!!" + , string2 = "这是一个测试, 等会看结果~!!!" + , lbool = [true, false, true, false] + , lint8 = [123, -123, 66, -66, 88, -88] + , luint8 = [1, 2, 3, 123, 67, 88] + , lint16 = [-12345, 12345, 6666, -6666] + , luint16 = [12345, 12345, 6666, 6666] + , lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666] + , luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666] + , lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666] + , luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666] + , linte8 = [123, 12, -66, 66, 34] + , linte16 = [12334, 12, -6666, 6666, 3412] + , linte32 = [12334, 12, -666666666, 6666, 3412] + , linte64 = [12334, 12, -666666666666, 6666, 3412] + , lnum8 = [123, 12.123, -66.456789, 66, 34] + , lnum16 = [12334, -12, -6666.6666, 6666, 3412] + , lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412] + , lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999] + , lnfloat32 = [-666666.88888, 4434.434, 434.43, 3434] + , lnfloat64 = [-666666666666.88888, 4434.434, 434.43, 11111111111.34343, 5566] + , lfloat = [1.1, 2.2, 3.3, 666666.666] + , ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666] + , lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式"/utf8>>] + , lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}] + }, + List = protoMsg:encodeIol(AllType), + iolist_to_binary(List), + %%io:format("~p~n", [List]), + AllType1 = protoMsg:decode(iolist_to_binary(List)). + %%AllType1. + +tall1(0) -> + ok; +tall1(N) -> + AllType = #allType{ + bool = false + , int8 = -66 + , uint8 = 66 + , int16 = -666 + , uint16 = 666 + , int32 = -66666 + , uint32 = 66666 + , int64 = -5294967296 + , uint64 = 5294967296 + , inte8 = -88 + , uinte8 = 88 + , inte16 = -8888 + , uinte16 = 8888 + , inte32 = -888888 + , uinte32 = 888888 + , inte64 = -8888888888 + , uinte64 = 8888888888 + , num8 = -99 + , unum8 = 99 + , num16 = -9999 + , unum16 = 9999 + , num32 = -999999 + , unum32 = 999999 + , num64 = -9999999999 + , unum64 = 9999999999 + , numfloat = 999999.99999 + , numdouble = 9999999999.99999 + , float = 123456.789321 + , double = 4300000000.789321 + , string1 = "this is a test!!!!" + , string2 = "这是一个测试, 等会看结果~!!!" + , lbool = [true, false, true, false] + , lint8 = [123, -123, 66, -66, 88, -88] + , luint8 = [1, 2, 3, 123, 67, 88] + , lint16 = [-12345, 12345, 6666, -6666] + , luint16 = [12345, 12345, 6666, 6666] + , lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666] + , luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666] + , lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666] + , luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666] + , linte8 = [123, 12, -66, 66, 34] + , linte16 = [12334, 12, -6666, 6666, 3412] + , linte32 = [12334, 12, -666666666, 6666, 3412] + , linte64 = [12334, 12, -666666666666, 6666, 3412] + , lnum8 = [123, 12.123, -66.456789, 66, 34] + , lnum16 = [12334, -12, -6666.6666, 6666, 3412] + , lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412] + , lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999] + , lnfloat32 = [-666666.88888, 4434.434, 434.43, 3434] + , lnfloat64 = [-666666666666.88888, 4434.434, 434.43, 11111111111.34343, 5566] + , lfloat = [1.1, 2.2, 3.3, 666666.666] + , ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666] + , lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式"/utf8>>] + , lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}] + }, + %protoMsg:encodeIol(AllType), + term_to_binary(AllType), + tall1(N - 1). + + +tall(N) -> + AllType = #allType{ + bool = false + , int8 = -66 + , uint8 = 66 + , int16 = -666 + , uint16 = 666 + , int32 = -66666 + , uint32 = 66666 + , int64 = -5294967296 + , uint64 = 5294967296 + , inte8 = -88 + , uinte8 = 88 + , inte16 = -8888 + , uinte16 = 8888 + , inte32 = -888888 + , uinte32 = 888888 + , inte64 = -8888888888 + , uinte64 = 8888888888 + , num8 = -99 + , unum8 = 99 + , num16 = -9999 + , unum16 = 9999 + , num32 = -999999 + , unum32 = 999999 + , num64 = -9999999999 + , unum64 = 9999999999 + , numfloat = 999999.99999 + , numdouble = 9999999999.99999 + , float = 123456.789321 + , double = 4300000000.789321 + , string1 = "this is a test!!!!" + , string2 = "这是一个测试, 等会看结果~!!!" + , lbool = [true, false, true, false] + , lint8 = [123, -123, 66, -66, 88, -88] + , luint8 = [1, 2, 3, 123, 67, 88] + , lint16 = [-12345, 12345, 6666, -6666] + , luint16 = [12345, 12345, 6666, 6666] + , lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666] + , luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666] + , lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666] + , luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666] + , linte8 = [123, 12, -66, 66, 34] + , linte16 = [12334, 12, -6666, 6666, 3412] + , linte32 = [12334, 12, -666666666, 6666, 3412] + , linte64 = [12334, 12, -666666666666, 6666, 3412] + , lnum8 = [123, 12.123, -66.456789, 66, 34] + , lnum16 = [12334, -12, -6666.6666, 6666, 3412] + , lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412] + , lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999] + , lnfloat32 = [-666666.88888, 4434.434, 434.43, 3434] + , lnfloat64 = [-666666666666.88888, 4434.434, 434.43, 11111111111.34343, 5566] + , lfloat = [1.1, 2.2, 3.3, 666666.666] + , ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666] + , lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式"/utf8>>] + , lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}] + }, + %List = protoMsg:encodeIol(AllType), + tall(N, term_to_binary(AllType)). + +tall(0, Bin) -> + Bin; +tall(N, Bin) -> + %protoMsg:decode(Bin), + binary_to_term(Bin), + tall(N - 1, Bin). + + +hh1(0) -> + ok; +hh1(N) -> + A = [tt1, tt2, tt3, tt4, {tet, tt1}, {tet, tt2}, {tet, tt3}, yyy], + test(A), + hh1(N - 1). + +hh2(0) -> + ok; +hh2(N) -> + A = [tt1, tt2, tt3, tt4, {tet, tt1}, {tet, tt2}, {tet, tt3}, yyy], + tet(A), + hh2(N - 1). + +test([]) -> + ok; +test([A | T]) -> + case A of + tt1 -> + ok; + tt2 -> + ok; + tt3 -> + ok; + tt4 -> + ok; + {tet, tt1} -> + ok; + {tet, tt2} -> + ok; + {tet, tt3} -> + ok; + _ -> + ok + end, + test(T). + + +tet([]) -> + ok; +tet([tt1 | T]) -> + ok, + tet(T); +tet([tt2 | T]) -> + ok, + tet(T); +tet([tt3 | T]) -> + ok, + tet(T); +tet([tt4 | T]) -> + ok, + tet(T); +tet([{tet, tt1} | T]) -> + ok, + tet(T); +tet([{tet, tt2} | T]) -> + ok, + tet(T); +tet([{tet, tt3} | T]) -> + ok, + tet(T); +tet([YY | T]) -> + ok, + tet(T). + +ttt11(N) -> + Add = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + ttt11(N, Add). + +ttt11(0, Add) -> + ok; +ttt11(N, Add) -> + protoMsg:encodeIol(Add), + ttt11(N - 1, Add). + +%%tt1(Add) -> + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + %%protoMsg:encodeIol(Add). +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%Bin = protoCode:encode(AddressBook), +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%MsgId = protoMsg:getMsgId(element(1, AddressBook)), +%<>. + + +tt(N) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + Bin = protoMsg:encodeIol(AddressBook), + %ok = file:write_file("fff.bin", Bin), + %print_bin(Bin), + tt(N, iolist_to_binary(Bin)). +tt(0, Bin) -> + protoMsg:decode(Bin); +tt(N, Bin) -> + protoMsg:decode(Bin), + tt(N - 1, Bin). + + +%{Len, List, RestBin} = protoMsg("AddressBook", Bin), +%io:format("Len:~p, RestBin:~p~n", [Len, RestBin]), +%io:format("List:~p~n", [List]), +%{Map, _, _} = sproto:decode2("AddressBook", Bin), +%Map. + + +ttt1(0) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "你好啊 嘿嘿"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "范德萨地方范德萨发"}, type = 3} + ] + } + ] + }, + term_to_binary(AddressBook); +ttt1(N) -> + ttt1(), + ttt1(N - 1). + +ttt1() -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "你好啊 嘿嘿"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "范德萨地方范德萨发"}, type = 3} + ] + } + ] + }, + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + Bin = term_to_binary(AddressBook). +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%Bin = protoCode:encode(AddressBook), +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%MsgId = protoMsg:getMsgId(element(1, AddressBook)), +%<>. + + +ttt(N) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + %ok = file:write_file("fff.bin", Bin), + %print_bin(Bin), + ttt(N, term_to_binary(AddressBook)). +ttt(0, Bin) -> + binary_to_term(Bin); +ttt(N, Bin) -> + binary_to_term(Bin), + ttt(N - 1, Bin). + +print_bin(Bin) -> + ByteList = lists:reverse(bin_to_hex(Bin, [])), + Fun = fun(Byte, Acc) -> + io:format("~2.16.0b ", [Byte]), + case Acc rem 8 =:= 0 of + true -> io:format("~n", []); + false -> ok + end, + Acc + 1 + end, + lists:foldl(Fun, 1, ByteList), + io:format("bytes:~p~n", [byte_size(Bin)]). + +bin_to_hex(<<>>, Acc) -> + Acc; +bin_to_hex(Bin, Acc) -> + <> = Bin, + bin_to_hex(Bin2, [A | Acc]). + +a1(B) -> + Bin = list_to_binary([X rem 256 || X <- lists:seq(1, 10000)]), + a1(B, Bin). + +a1(0, Bin) -> + <> = Bin, + [X || <> <= ListBin]; +a1(N, Bin) -> + <> = Bin, + A = [X || <> <= ListBin], + B = [X || <> <= ListBin], + io:format("IMY********** ~p~n", [A == B]), + a1(N - 1, Bin). + +a2(B) -> + Bin = list_to_binary([X rem 256 || X <- lists:seq(1, 10000)]), + a2(B, Bin). + +a2(0, Bin) -> + Len = 200, + <> = Bin, + [X || <> <= ListBin]; +a2(N, Bin) -> + Len = 200, + <> = Bin, + [X || <> <= ListBin], + a2(N - 1, Bin). \ No newline at end of file diff --git a/test/v1/protoMsg.erl b/test/v1/protoMsg.erl new file mode 100644 index 0000000..630dbb9 --- /dev/null +++ b/test/v1/protoMsg.erl @@ -0,0 +1,842 @@ +-module(protoMsg). + + +-compile([nowarn_unused_vars]). + +-export([encode/1, decode/1, encodeRec/1, decodeBin/2]). + +-define(min8, -128). +-define(max8, 127). +-define(min16, -32768). +-define(max16, 32767). +-define(min32, -2147483648). +-define(max32, 2147483647). +-define(min64, -9223372036854775808). +-define(max64, 9223372036854775807). + +-define(minF32, -3.4E+38). +-define(maxF32, 3.4E+38). +-define(minF64, -1.797E-308). +-define(maxF64, 1.797E+308). + +-define(int8(V), <>). +-define(uint8(V), <>). +-define(int16(V), <>). +-define(uint16(V), <>). +-define(int32(V), <>). +-define(uint32(V), <>). +-define(int64(V), <>). +-define(uint64(V), <>). +-define(integer(V), (integer(V))). +-define(number(V), (number(V))). +-define(string(V), (string(V))). +-define(float(V), <>). +-define(double(V), <>). +-define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)). +-define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)). +-define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]). +-define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]). +-define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]). +-define(list_int16(List), [<<(length(List)):16/big>>, [?int16(V) || V <- List]]). +-define(list_uint16(List), [<<(length(List)):16/big>>, [?uint16(V) || V <- List]]). +-define(list_int32(List), [<<(length(List)):16/big>>, [?int32(V) || V <- List]]). +-define(list_uint32(List), [<<(length(List)):16/big>>, [?uint32(V) || V <- List]]). +-define(list_int64(List), [<<(length(List)):16/big>>, [?int64(V) || V <- List]]). +-define(list_uint64(List), [<<(length(List)):16/big>>, [?uint64(V) || V <- List]]). +-define(list_float(List), [<<(length(List)):16/big>>, [?float(V) || V <- List]]). +-define(list_double(List), [<<(length(List)):16/big>>, [?double(V) || V <- List]]). +-define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]). +-define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]). +-define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]). +-define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]). + +-define(BinaryShareSize, 65). +-define(BinaryCopyRatio, 1.2). + +integer(V) -> + if + V >= ?min8 andalso V =< ?max8 -> + <<8:8, <>/binary>>; + V >= ?min16 andalso V =< ?max16 -> + <<16:8, <>/binary>>; + V >= ?min32 andalso V =< ?max32 -> + <<32:8, <>/binary>>; + V >= ?min64 andalso V =< ?max64 -> + <<64:8, <>/binary>>; + true -> + throw(exceeded_the_integer) + end. + +number(V) -> + if + erlang:is_integer(V) -> + if + V >= ?min8 andalso V =< ?max8 -> + <<8:8, <>/binary>>; + V >= ?min16 andalso V =< ?max16 -> + <<16:8, <>/binary>>; + V >= ?min32 andalso V =< ?max32 -> + <<32:8, <>/binary>>; + V >= ?min64 andalso V =< ?max64 -> + <<64:8, <>/binary>>; + true -> + throw(exceeded_the_integer) + end; + erlang:is_float(V) -> + if + V >= ?minF32 andalso V =< ?maxF32 -> + <<33:8, <>/binary>>; + V >= ?minF64 andalso V =< ?maxF64 -> + <<65:8, <>/binary>>; + true -> + throw(exceeded_the_float) + end; + true -> + throw(is_not_number) + end. + +string(Str) when is_binary(Str) -> + [<<(byte_size(Str)):16/big>>, Str]; +string(Str) -> + Str2 = unicode:characters_to_binary(Str, utf8), + [<<(byte_size(Str2)):16/big>>, Str2]. + +decode(Bin) -> + <> = Bin, + decodeBin(MsgId, MsgBin). + +deIntegerList(0, MsgBin, RetList) -> + {lists:reverse(RetList), MsgBin}; +deIntegerList(N, MsgBin, RetList) -> + <> = MsgBin, + deIntegerList(N - 1, LeftBin, [Int | RetList]). + +deNumberList(0, MsgBin, RetList) -> + {lists:reverse(RetList), MsgBin}; +deNumberList(N, MsgBin, RetList) -> + <> = MsgBin, + case NumBits of + 33 -> + <> = NumBin, + deNumberList(N - 1, LeftBin, [Float | RetList]); + 65 -> + <> = NumBin, + deNumberList(N - 1, LeftBin, [Float | RetList]); + _ -> + <> = NumBin, + deNumberList(N - 1, LeftBin, [Int | RetList]) + end. + +deStringList(0, MsgBin, _RefSize, RetList) -> + {lists:reverse(RetList), MsgBin}; +deStringList(N, MsgBin, RefSize, RetList) -> + <> = MsgBin, + case Len < ?BinaryShareSize of + true -> + deStringList(N - 1, LeftBin, RefSize, [StrBin | RetList]); + _ -> + case RefSize / Len > ?BinaryCopyRatio of + true -> + StrBinCopy = binary:copy(StrBin), + deStringList(N - 1, LeftBin, RefSize, [StrBinCopy | RetList]); + _ -> + deStringList(N - 1, LeftBin, RefSize, [StrBin | RetList]) + end + end. + +deRecordList(0, _MsgId, MsgBin, RetList) -> + {lists:reverse(RetList), MsgBin}; +deRecordList(N, MsgId, MsgBin, RetList) -> + {Tuple, LeftBin} = decodeRec(MsgId, MsgBin), + deRecordList(N - 1, MsgId, LeftBin, [Tuple | RetList]). + +encodeRec({test, V1}) -> + [?string(V1)]; +encodeRec({phoneNumber, V1, V2}) -> + [?record(V1), ?int32(V2)]; +encodeRec({person, V1, V2, V3, V4}) -> + [?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; +encodeRec({union, V1, V2}) -> + [?string(V1), ?int32(V2)]; +encodeRec(_) -> + []. + +encode({test, V1}) -> + [<<1:16/big-unsigned>>, ?string(V1)]; +encode({phoneNumber, V1, V2}) -> + [<<2:16/big-unsigned>>, ?record(V1), ?int32(V2)]; +encode({person, V1, V2, V3, V4}) -> + [<<3:16/big-unsigned>>, ?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; +encode({addressBook, V1, V2}) -> + [<<4:16/big-unsigned>>, ?list_record(V1), ?list_record(V2)]; +encode({union, V1, V2}) -> + [<<5:16/big-unsigned>>, ?string(V1), ?int32(V2)]; +encode({tbool, V1}) -> + [<<6:16/big-unsigned>>, ?bool(V1)]; +encode({tint8, V1, V2}) -> + [<<7:16/big-unsigned>>, ?int8(V1), ?int8(V2)]; +encode({tuint8, V1, V2}) -> + [<<8:16/big-unsigned>>, ?uint8(V1), ?uint8(V2)]; +encode({tint16, V1, V2}) -> + [<<9:16/big-unsigned>>, ?int16(V1), ?int16(V2)]; +encode({tuint16, V1, V2}) -> + [<<10:16/big-unsigned>>, ?uint16(V1), ?uint16(V2)]; +encode({tint32, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) -> + [<<11:16/big-unsigned>>, ?int32(V1), ?int32(V2), ?int32(V3), ?int32(V4), ?int32(V5), ?int32(V6), ?int32(V7), ?int32(V8), ?int32(V9), ?int32(V10)]; +encode({tuint32, V1, V2}) -> + [<<12:16/big-unsigned>>, ?uint32(V1), ?uint32(V2)]; +encode({tint64, V1, V2}) -> + [<<13:16/big-unsigned>>, ?int64(V1), ?int64(V2)]; +encode({tuint64, V1, V2}) -> + [<<14:16/big-unsigned>>, ?uint64(V1), ?uint64(V2)]; +encode({tinteger, V1, V2, V3, V4, V5, V6, V7, V8}) -> + [<<15:16/big-unsigned>>, ?integer(V1), ?integer(V2), ?integer(V3), ?integer(V4), ?integer(V5), ?integer(V6), ?integer(V7), ?integer(V8)]; +encode({tnumber, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) -> + [<<16:16/big-unsigned>>, ?number(V1), ?number(V2), ?number(V3), ?number(V4), ?number(V5), ?number(V6), ?number(V7), ?number(V8), ?number(V9), ?number(V10)]; +encode({tfloat, V1, V2}) -> + [<<17:16/big-unsigned>>, ?float(V1), ?float(V2)]; +encode({tdouble, V1, V2}) -> + [<<18:16/big-unsigned>>, ?double(V1), ?double(V2)]; +encode({tstring, V1, V2}) -> + [<<19:16/big-unsigned>>, ?string(V1), ?string(V2)]; +encode({tlistbool, V1}) -> + [<<20:16/big-unsigned>>, ?list_bool(V1)]; +encode({tlistint8, V1}) -> + [<<21:16/big-unsigned>>, ?list_int8(V1)]; +encode({tlistuint8, V1}) -> + [<<22:16/big-unsigned>>, ?list_uint8(V1)]; +encode({tlistint16, V1}) -> + [<<23:16/big-unsigned>>, ?list_int16(V1)]; +encode({tlistuint16, V1}) -> + [<<24:16/big-unsigned>>, ?list_uint16(V1)]; +encode({tlistint32, V1}) -> + [<<25:16/big-unsigned>>, ?list_int32(V1)]; +encode({tlistuint32, V1}) -> + [<<26:16/big-unsigned>>, ?list_uint32(V1)]; +encode({tlistint64, V1}) -> + [<<27:16/big-unsigned>>, ?list_int64(V1)]; +encode({tlistuint64, V1}) -> + [<<28:16/big-unsigned>>, ?list_uint64(V1)]; +encode({tlistinteger, V1}) -> + [<<29:16/big-unsigned>>, ?list_integer(V1)]; +encode({tlistnumber, V1}) -> + [<<30:16/big-unsigned>>, ?list_number(V1)]; +encode({tlistfloat, V1}) -> + [<<31:16/big-unsigned>>, ?list_float(V1)]; +encode({tlistdouble, V1}) -> + [<<32:16/big-unsigned>>, ?list_double(V1)]; +encode({tliststring, V1}) -> + [<<33:16/big-unsigned>>, ?list_string(V1)]; +encode({tlistunion, V1}) -> + [<<34:16/big-unsigned>>, ?list_record(V1)]; +encode({allType, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}) -> + [<<35:16/big-unsigned>>, ?bool(V1), ?int8(V2), ?uint8(V3), ?int16(V4), ?uint16(V5), ?int32(V6), ?uint32(V7), ?int64(V8), ?uint64(V9), ?integer(V10), ?integer(V11), ?integer(V12), ?integer(V13), ?integer(V14), ?integer(V15), ?integer(V16), ?integer(V17), ?number(V18), ?number(V19), ?number(V20), ?number(V21), ?number(V22), ?number(V23), ?number(V24), ?number(V25), ?number(V26), ?number(V27), ?float(V28), ?double(V29), ?string(V30), ?string(V31), ?record(V32), ?list_bool(V33), ?list_int8(V34), ?list_uint8(V35), ?list_int16(V36), ?list_uint16(V37), ?list_int32(V38), ?list_uint32(V39), ?list_int64(V40), ?list_uint64(V41), ?list_integer(V42), ?list_integer(V43), ?list_integer(V44), ?list_integer(V45), ?list_number(V46), ?list_number(V47), ?list_number(V48), ?list_number(V49), ?list_number(V50), ?list_number(V51), ?list_float(V52), ?list_double(V53), ?list_string(V54), ?list_record(V55)]; +encode({person1, V1, V2, V3, V4}) -> + [<<1001:16/big-unsigned>>, ?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)]; +encode(_) -> + []. + +decodeRec(1, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + MsgRec = {test, V1}, + {MsgRec, LeftBin1}; +decodeRec(2, LeftBin0) -> + <> = LeftBin0, + case IsUndef1 of + 0 -> + V1 = undefined, + LeftBin2 = LeftBin1 ; + _ -> + {V1, LeftBin2} = decodeRec(1, LeftBin1) + end, + <> = LeftBin2, + MsgRec = {phoneNumber, V1, V2}, + {MsgRec, LeftBin3}; +decodeRec(3, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + <> = LeftBin2, + case Len2 < ?BinaryShareSize of + true -> + V3 = TemStrV3; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V3 = binary:copy(TemStrV3); + _ -> + V3 = TemStrV3 + end + end, + <> = LeftBin3, + {V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []), + MsgRec = {person, V1, V2, V3, V4}, + {MsgRec, LeftBin5}; +decodeRec(5, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + MsgRec = {union, V1, V2}, + {MsgRec, LeftBin2}; +decodeRec(_, _) -> + {{}, <<>>}. + +decodeBin(1, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + {test, V1}; +decodeBin(2, LeftBin0) -> + <> = LeftBin0, + case IsUndef1 of + 0 -> + V1 = undefined, + LeftBin2 = LeftBin1 ; + _ -> + {V1, LeftBin2} = decodeRec(1, LeftBin1) + end, + <> = LeftBin2, + {phoneNumber, V1, V2}; +decodeBin(3, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + <> = LeftBin2, + case Len2 < ?BinaryShareSize of + true -> + V3 = TemStrV3; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V3 = binary:copy(TemStrV3); + _ -> + V3 = TemStrV3 + end + end, + <> = LeftBin3, + {V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []), + {person, V1, V2, V3, V4}; +decodeBin(4, LeftBin0) -> + <> = LeftBin0, + {V1, LeftBin2} = deRecordList(Len1, 3, LeftBin1, []), + <> = LeftBin2, + {V2, LeftBin4} = deRecordList(Len2, 3, LeftBin3, []), + {addressBook, V1, V2}; +decodeBin(5, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + {union, V1, V2}; +decodeBin(6, LeftBin0) -> + <> = LeftBin0, + V1 = Bool1 =:= 1, + {tbool, V1}; +decodeBin(7, LeftBin0) -> + <> = LeftBin0, + {tint8, V1, V2}; +decodeBin(8, LeftBin0) -> + <> = LeftBin0, + {tuint8, V1, V2}; +decodeBin(9, LeftBin0) -> + <> = LeftBin0, + {tint16, V1, V2}; +decodeBin(10, LeftBin0) -> + <> = LeftBin0, + {tuint16, V1, V2}; +decodeBin(11, LeftBin0) -> + <> = LeftBin0, + {tint32, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}; +decodeBin(12, LeftBin0) -> + <> = LeftBin0, + {tuint32, V1, V2}; +decodeBin(13, LeftBin0) -> + <> = LeftBin0, + {tint64, V1, V2}; +decodeBin(14, LeftBin0) -> + <> = LeftBin0, + {tuint64, V1, V2}; +decodeBin(15, LeftBin0) -> + <> = LeftBin0, + {tinteger, V1, V2, V3, V4, V5, V6, V7, V8}; +decodeBin(16, LeftBin0) -> + <> = LeftBin0, + case NumBits1 of + 33-> + <> = LeftBin1; + 65 -> + <> = LeftBin1; + _ -> + <> = LeftBin1 + end, + <> = LeftBin2, + case NumBits2 of + 33-> + <> = LeftBin3; + 65 -> + <> = LeftBin3; + _ -> + <> = LeftBin3 + end, + <> = LeftBin4, + case NumBits3 of + 33-> + <> = LeftBin5; + 65 -> + <> = LeftBin5; + _ -> + <> = LeftBin5 + end, + <> = LeftBin6, + case NumBits4 of + 33-> + <> = LeftBin7; + 65 -> + <> = LeftBin7; + _ -> + <> = LeftBin7 + end, + <> = LeftBin8, + case NumBits5 of + 33-> + <> = LeftBin9; + 65 -> + <> = LeftBin9; + _ -> + <> = LeftBin9 + end, + <> = LeftBin10, + case NumBits6 of + 33-> + <> = LeftBin11; + 65 -> + <> = LeftBin11; + _ -> + <> = LeftBin11 + end, + <> = LeftBin12, + case NumBits7 of + 33-> + <> = LeftBin13; + 65 -> + <> = LeftBin13; + _ -> + <> = LeftBin13 + end, + <> = LeftBin14, + case NumBits8 of + 33-> + <> = LeftBin15; + 65 -> + <> = LeftBin15; + _ -> + <> = LeftBin15 + end, + <> = LeftBin16, + case NumBits9 of + 33-> + <> = LeftBin17; + 65 -> + <> = LeftBin17; + _ -> + <> = LeftBin17 + end, + <> = LeftBin18, + case NumBits10 of + 33-> + <> = LeftBin19; + 65 -> + <> = LeftBin19; + _ -> + <> = LeftBin19 + end, + {tnumber, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}; +decodeBin(17, LeftBin0) -> + <> = LeftBin0, + {tfloat, V1, V2}; +decodeBin(18, LeftBin0) -> + <> = LeftBin0, + {tdouble, V1, V2}; +decodeBin(19, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + case Len2 < ?BinaryShareSize of + true -> + V2 = TemStrV2; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V2 = binary:copy(TemStrV2); + _ -> + V2 = TemStrV2 + end + end, + {tstring, V1, V2}; +decodeBin(20, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV =:= 1 || <> <= ListBin1], + {tlistbool, V1}; +decodeBin(21, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistint8, V1}; +decodeBin(22, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistuint8, V1}; +decodeBin(23, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistint16, V1}; +decodeBin(24, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistuint16, V1}; +decodeBin(25, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistint32, V1}; +decodeBin(26, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistuint32, V1}; +decodeBin(27, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistint64, V1}; +decodeBin(28, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistuint64, V1}; +decodeBin(29, LeftBin0) -> + <> = LeftBin0, + {V1, LeftBin2} = deIntegerList(Len1, LeftBin1, []), + {tlistinteger, V1}; +decodeBin(30, LeftBin0) -> + <> = LeftBin0, + {V1, LeftBin2} = deNumberList(Len1, LeftBin1, []), + {tlistnumber, V1}; +decodeBin(31, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistfloat, V1}; +decodeBin(32, LeftBin0) -> + <> = LeftBin0, + <> = LeftBin1, + V1 = [TemV || <> <= ListBin1], + {tlistdouble, V1}; +decodeBin(33, LeftBin0) -> + <> = LeftBin0, + RefSize = binary:referenced_byte_size(LeftBin0), + {V1, LeftBin2} = deStringList(Len1, LeftBin1, RefSize, []), + {tliststring, V1}; +decodeBin(34, LeftBin0) -> + <> = LeftBin0, + {V1, LeftBin2} = deRecordList(Len1, 5, LeftBin1, []), + {tlistunion, V1}; +decodeBin(35, LeftBin0) -> + <> = LeftBin0, + V1 = Bool1 =:= 1, + <> = LeftBin1, + <> = LeftBin2, + case NumBits1 of + 33-> + <> = LeftBin3; + 65 -> + <> = LeftBin3; + _ -> + <> = LeftBin3 + end, + <> = LeftBin4, + case NumBits2 of + 33-> + <> = LeftBin5; + 65 -> + <> = LeftBin5; + _ -> + <> = LeftBin5 + end, + <> = LeftBin6, + case NumBits3 of + 33-> + <> = LeftBin7; + 65 -> + <> = LeftBin7; + _ -> + <> = LeftBin7 + end, + <> = LeftBin8, + case NumBits4 of + 33-> + <> = LeftBin9; + 65 -> + <> = LeftBin9; + _ -> + <> = LeftBin9 + end, + <> = LeftBin10, + case NumBits5 of + 33-> + <> = LeftBin11; + 65 -> + <> = LeftBin11; + _ -> + <> = LeftBin11 + end, + <> = LeftBin12, + case NumBits6 of + 33-> + <> = LeftBin13; + 65 -> + <> = LeftBin13; + _ -> + <> = LeftBin13 + end, + <> = LeftBin14, + case NumBits7 of + 33-> + <> = LeftBin15; + 65 -> + <> = LeftBin15; + _ -> + <> = LeftBin15 + end, + <> = LeftBin16, + case NumBits8 of + 33-> + <> = LeftBin17; + 65 -> + <> = LeftBin17; + _ -> + <> = LeftBin17 + end, + <> = LeftBin18, + case NumBits9 of + 33-> + <> = LeftBin19; + 65 -> + <> = LeftBin19; + _ -> + <> = LeftBin19 + end, + <> = LeftBin20, + case NumBits10 of + 33-> + <> = LeftBin21; + 65 -> + <> = LeftBin21; + _ -> + <> = LeftBin21 + end, + <> = LeftBin22, + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin23, + case Len1 < ?BinaryShareSize of + true -> + V30 = TemStrV30; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V30 = binary:copy(TemStrV30); + _ -> + V30 = TemStrV30 + end + end, + <> = LeftBin24, + case Len2 < ?BinaryShareSize of + true -> + V31 = TemStrV31; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V31 = binary:copy(TemStrV31); + _ -> + V31 = TemStrV31 + end + end, + <> = LeftBin25, + case IsUndef1 of + 0 -> + V32 = undefined, + LeftBin27 = LeftBin26 ; + _ -> + {V32, LeftBin27} = decodeRec(5, LeftBin26) + end, + <> = LeftBin27, + <> = LeftBin28, + V33 = [TemV =:= 1 || <> <= ListBin1], + <> = LeftBin29, + <> = LeftBin30, + V34 = [TemV || <> <= ListBin2], + <> = LeftBin31, + <> = LeftBin32, + V35 = [TemV || <> <= ListBin3], + <> = LeftBin33, + <> = LeftBin34, + V36 = [TemV || <> <= ListBin4], + <> = LeftBin35, + <> = LeftBin36, + V37 = [TemV || <> <= ListBin5], + <> = LeftBin37, + <> = LeftBin38, + V38 = [TemV || <> <= ListBin6], + <> = LeftBin39, + <> = LeftBin40, + V39 = [TemV || <> <= ListBin7], + <> = LeftBin41, + <> = LeftBin42, + V40 = [TemV || <> <= ListBin8], + <> = LeftBin43, + <> = LeftBin44, + V41 = [TemV || <> <= ListBin9], + <> = LeftBin45, + {V42, LeftBin47} = deIntegerList(Len12, LeftBin46, []), + <> = LeftBin47, + {V43, LeftBin49} = deIntegerList(Len13, LeftBin48, []), + <> = LeftBin49, + {V44, LeftBin51} = deIntegerList(Len14, LeftBin50, []), + <> = LeftBin51, + {V45, LeftBin53} = deIntegerList(Len15, LeftBin52, []), + <> = LeftBin53, + {V46, LeftBin55} = deNumberList(Len16, LeftBin54, []), + <> = LeftBin55, + {V47, LeftBin57} = deNumberList(Len17, LeftBin56, []), + <> = LeftBin57, + {V48, LeftBin59} = deNumberList(Len18, LeftBin58, []), + <> = LeftBin59, + {V49, LeftBin61} = deNumberList(Len19, LeftBin60, []), + <> = LeftBin61, + {V50, LeftBin63} = deNumberList(Len20, LeftBin62, []), + <> = LeftBin63, + {V51, LeftBin65} = deNumberList(Len21, LeftBin64, []), + <> = LeftBin65, + <> = LeftBin66, + V52 = [TemV || <> <= ListBin20], + <> = LeftBin67, + <> = LeftBin68, + V53 = [TemV || <> <= ListBin21], + <> = LeftBin69, + {V54, LeftBin71} = deStringList(Len24, LeftBin70, RefSize, []), + <> = LeftBin71, + {V55, LeftBin73} = deRecordList(Len25, 5, LeftBin72, []), + {allType, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}; +decodeBin(1001, LeftBin0) -> + RefSize = binary:referenced_byte_size(LeftBin0), + <> = LeftBin0, + case Len1 < ?BinaryShareSize of + true -> + V1 = TemStrV1; + _ -> + case RefSize / Len1 > ?BinaryCopyRatio of + true -> + V1 = binary:copy(TemStrV1); + _ -> + V1 = TemStrV1 + end + end, + <> = LeftBin1, + <> = LeftBin2, + case Len2 < ?BinaryShareSize of + true -> + V3 = TemStrV3; + _ -> + case RefSize / Len2 > ?BinaryCopyRatio of + true -> + V3 = binary:copy(TemStrV3); + _ -> + V3 = TemStrV3 + end + end, + <> = LeftBin3, + {V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []), + {person1, V1, V2, V3, V4}; +decodeBin(_, _) -> + {{}, <<>>}. + diff --git a/test/v1/protoMsg.hrl b/test/v1/protoMsg.hrl new file mode 100644 index 0000000..23f2743 --- /dev/null +++ b/test/v1/protoMsg.hrl @@ -0,0 +1,226 @@ +-opaque int8() :: -128..127. +-opaque int16() :: -32768..32767. +-opaque int32() :: -2147483648..2147483647. +-opaque int64() :: -9223372036854775808..9223372036854775807. +-opaque uint8() :: 0..255. +-opaque uint16() :: 0..65536. +-opaque uint32() :: 0..4294967295. +-opaque uint64() :: 0..18446744073709551615. +-opaque double() :: float(). + +-define(ERR1, 1). %% 辅导费 +-define(ERR2, 2). %% 444 +-define(ERR3, 3). %% 辅导费 +-define(ERR4, 4). %% dfsf +-define(ERR5, 5). %% 其他注释辅导费 err6:dfff +-define(ERR7, 6). %% def +-define(ERR8, 7). %% 其他注释辅导费 +-define(ERR6, 1001). %% dfff + + +-record(test, { + aa = "" :: string() +}). +-record(phoneNumber, { + number = undefined :: #test{} + , type = 0 :: int32() +}). +-record(person, { + name = "" :: string() + , id = 0 :: int32() + , email = "" :: string() + , phone = [] :: [#phoneNumber{}] +}). +-record(addressBook, { + person = [] :: [#person{}] + , other = [] :: [#person{}] +}). +-record(union, { + test = "" :: string() + , type = 0 :: int32() +}). +-record(tbool, { + bool = false :: boolean() +}). +-record(tint8, { + int1 = 0 :: int8() + , int2 = 0 :: int8() +}). +-record(tuint8, { + int1 = 0 :: uint8() + , int2 = 0 :: uint8() +}). +-record(tint16, { + int1 = 0 :: int16() + , int2 = 0 :: int16() +}). +-record(tuint16, { + int1 = 0 :: uint16() + , int2 = 0 :: uint16() +}). +-record(tint32, { + int1 = 0 :: int32() + , int2 = 0 :: int32() + , int3 = 0 :: int32() + , int4 = 0 :: int32() + , int5 = 0 :: int32() + , int6 = 0 :: int32() + , int7 = 0 :: int32() + , int8 = 0 :: int32() + , int9 = 0 :: int32() + , int10 = 0 :: int32() +}). +-record(tuint32, { + int1 = 0 :: uint32() + , int2 = 0 :: uint32() +}). +-record(tint64, { + int1 = 0 :: int64() + , int2 = 0 :: int64() +}). +-record(tuint64, { + int1 = 0 :: uint64() + , int2 = 0 :: uint64() +}). +-record(tinteger, { + int1 = 0 :: integer() + , int2 = 0 :: integer() + , int3 = 0 :: integer() + , int4 = 0 :: integer() + , int5 = 0 :: integer() + , int6 = 0 :: integer() + , int7 = 0 :: integer() + , int8 = 0 :: integer() +}). +-record(tnumber, { + int1 = 0 :: number() + , int2 = 0 :: number() + , int3 = 0 :: number() + , int4 = 0 :: number() + , int5 = 0 :: number() + , int6 = 0 :: number() + , int7 = 0 :: number() + , int8 = 0 :: number() + , float1 = 0 :: number() + , float2 = 0 :: number() +}). +-record(tfloat, { + int1 = 0.0 :: float() + , int2 = 0.0 :: float() +}). +-record(tdouble, { + int1 = 0.0 :: double() + , int2 = 0.0 :: double() +}). +-record(tstring, { + int1 = "" :: string() + , int2 = "" :: string() +}). +-record(tlistbool, { + int1 = [] :: [boolean()] +}). +-record(tlistint8, { + int1 = [] :: [int8()] +}). +-record(tlistuint8, { + int1 = [] :: [uint8()] +}). +-record(tlistint16, { + int1 = [] :: [int16()] +}). +-record(tlistuint16, { + int1 = [] :: [uint16()] +}). +-record(tlistint32, { + int1 = [] :: [int32()] +}). +-record(tlistuint32, { + int1 = [] :: [uint32()] +}). +-record(tlistint64, { + int1 = [] :: [int64()] +}). +-record(tlistuint64, { + int1 = [] :: [uint64()] +}). +-record(tlistinteger, { + int1 = [] :: [integer()] +}). +-record(tlistnumber, { + int1 = [] :: [number()] +}). +-record(tlistfloat, { + int1 = [] :: [float()] +}). +-record(tlistdouble, { + int1 = [] :: [double()] +}). +-record(tliststring, { + int1 = [] :: [string()] +}). +-record(tlistunion, { + int1 = [] :: [#union{}] +}). +-record(allType, { + bool = false :: boolean() + , int8 = 0 :: int8() + , uint8 = 0 :: uint8() + , int16 = 0 :: int16() + , uint16 = 0 :: uint16() + , int32 = 0 :: int32() + , uint32 = 0 :: uint32() + , int64 = 0 :: int64() + , uint64 = 0 :: uint64() + , inte8 = 0 :: integer() + , uinte8 = 0 :: integer() + , inte16 = 0 :: integer() + , uinte16 = 0 :: integer() + , inte32 = 0 :: integer() + , uinte32 = 0 :: integer() + , inte64 = 0 :: integer() + , uinte64 = 0 :: integer() + , num8 = 0 :: number() + , unum8 = 0 :: number() + , num16 = 0 :: number() + , unum16 = 0 :: number() + , num32 = 0 :: number() + , unum32 = 0 :: number() + , num64 = 0 :: number() + , unum64 = 0 :: number() + , numfloat = 0 :: number() + , numdouble = 0 :: number() + , float = 0.0 :: float() + , double = 0.0 :: double() + , string1 = "" :: string() + , string2 = "" :: string() + , union = undefined :: #union{} + , lbool = [] :: [boolean()] + , lint8 = [] :: [int8()] + , luint8 = [] :: [uint8()] + , lint16 = [] :: [int16()] + , luint16 = [] :: [uint16()] + , lint32 = [] :: [int32()] + , luint32 = [] :: [uint32()] + , lint64 = [] :: [int64()] + , luint64 = [] :: [uint64()] + , linte8 = [] :: [integer()] + , linte16 = [] :: [integer()] + , linte32 = [] :: [integer()] + , linte64 = [] :: [integer()] + , lnum8 = [] :: [number()] + , lnum16 = [] :: [number()] + , lnum32 = [] :: [number()] + , lnum64 = [] :: [number()] + , lnfloat32 = [] :: [number()] + , lnfloat64 = [] :: [number()] + , lfloat = [] :: [float()] + , ldouble = [] :: [double()] + , lstring = [] :: [string()] + , lunion = [] :: [#union{}] +}). +-record(person1, { + name = "" :: string() + , id = 0 :: int32() + , email = "" :: string() + , phone = [] :: [#phoneNumber{}] +}). diff --git a/test/v1/start.bat b/test/v1/start.bat new file mode 100644 index 0000000..59d0635 --- /dev/null +++ b/test/v1/start.bat @@ -0,0 +1 @@ +start werl.exe \ No newline at end of file diff --git a/test/v1/test1.erl b/test/v1/test1.erl new file mode 100644 index 0000000..b3d4bf8 --- /dev/null +++ b/test/v1/test1.erl @@ -0,0 +1,672 @@ +-module(test1). + +-include("protoMsg.hrl"). +-compile(export_all). + +encode_int32(N) -> + TT = #tint32{int1 = 1, int2 = -1, int3 = 128, int4 = -128, int5 = 65536, + int6 = -65536, int7 = 2100000000, int8 = -2100000000, int9 = 678665, int10 = -678665}, + tt1(N, TT). + +tt1(0, TT) -> + ok; +tt1(N, TT) -> + protoMsg:encode(TT), + tt1(N - 1, TT). + +decode_int32(N) -> + TT = #tint32{int1 = 1, int2 = -1, int3 = 128, int4 = -128, int5 = 65536, + int6 = -65536, int7 = 2100000000, int8 = -2100000000, int9 = 678665, int10 = -678665}, + Bin = protoMsg:encode(TT), + tt2(N, iolist_to_binary(Bin)). + +tt2(0, Bin) -> + {protoMsg:decode(Bin), Bin}; +tt2(N, Bin) -> + protoMsg:decode(Bin), + tt2(N - 1, Bin). + +encode_addressBook(N) -> + Add = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + tt3(N, Add). + +tt3(0, Add) -> + ok; +tt3(N, Add) -> + protoMsg:encode(Add), + tt3(N - 1, Add). + + +decode_addressBook(N) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + Bin = protoMsg:encode(AddressBook), + tt4(N, iolist_to_binary(Bin)). +tt4(0, Bin) -> + Bin; +tt4(N, Bin) -> + protoMsg:decode(Bin), + tt4(N - 1, Bin). + +test1() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tbool{bool = true}))). + +test21() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint8{int1 = 123, int2 = -22}))). + +test22() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint8{int1 = 123, int2 = 182}))). + +test31() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint16{int1 = 12343, int2 = -3422}))). +test32() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint16{int1 = 43244, int2 = 43243}))). + +test41() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint32{int1 = 12343434, int2 = -34434322}))). + + +test42() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint32{int1 = 432444343, int2 = 432443433}))). + +test51() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint64{int1 = 12344343434, int2 = -344343434322}))). +test52() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint64{int1 = 4343432444343, int2 = 4324434343433}))). + +tt6(N) -> + Bin = iolist_to_binary(protoMsg:encode(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434})), + <<_MsgId:16/big, MsgBin/binary>> = Bin, + test6(N, MsgBin). + +test6(0, Bin) -> + io:format("IMY******111 ~p~n", [protoMsg:decode(Bin)]); +test6(N, Bin) -> + protoMsg:decodeBin(15, Bin), + test6(N - 1, Bin). + +tt66(N) -> + Bin = iolist_to_binary(protoMsg:encode(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434})), + test66(N, Bin). + +test66(0, Bin) -> + <<_MsgId:16/big, MsgBin/binary>> = Bin, + <> = MsgBin, + A = {tinteger, Int1, Int2, Int3, Int4, Int5, Int6, Int7, Int8}, + io:format("IMY******111 ~p~n", [A]); +test66(N, Bin) -> + <<_MsgId:16/big, MsgBin/binary>> = Bin, + %% <> = MsgBin, + %% {tinteger, Int1, Int2, Int3, Int4, Int5, Int6, Int7, Int8}, + <> = MsgBin, + {tinteger, V1, V2, V3, V4, V5, V6, V7, V8}, + test66(N - 1, Bin). + +tt67(N) -> + Bin = iolist_to_binary(protoMsg:encode(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434})), + test67(N, Bin). + +test67(0, Bin) -> + A = protoMsg:decode(Bin), + io:format("IMY******111 ~p~n", [A]); +test67(N, Bin) -> + _A = protoMsg:decode(Bin), + test67(N - 1, Bin). + +test7() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tnumber{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434, float1 = -34234343.343, float2 = 43242342342342.434}))). + +test81() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tfloat{int1 = -34234343.343, int2 = 42342342.434}))). +test82() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tdouble{int1 = -342343433333.343, int2 = 423423423333.434}))). + +test9() -> + protoMsg:decode(iolist_to_binary(protoMsg:encode(#tstring{int1 = "dfdf143242", int2 = "发地方撒发送"}))). + +allType() -> + AllType = #allType{ + bool = false + , int8 = -66 + , uint8 = 66 + , int16 = -666 + , uint16 = 666 + , int32 = -66666 + , uint32 = 66666 + , int64 = -5294967296 + , uint64 = 5294967296 + , inte8 = -88 + , uinte8 = 88 + , inte16 = -8888 + , uinte16 = 8888 + , inte32 = -888888 + , uinte32 = 888888 + , inte64 = -8888888888 + , uinte64 = 8888888888 + , num8 = -99 + , unum8 = 99 + , num16 = -9999 + , unum16 = 9999 + , num32 = -999999 + , unum32 = 999999 + , num64 = -9999999999 + , unum64 = 9999999999 + , numfloat = 999999.99999 + , numdouble = 9999999999.99999 + , float = 123456.789321 + , double = 4300000000.789321 + , string1 = "this is a test!!!!" + , string2 = "这是一个测试, 等会看结果~!!!" + , lbool = [true, false, true, false] + , lint8 = [123, -123, 66, -66, 88, -88] + , luint8 = [1, 2, 3, 123, 67, 88] + , lint16 = [-12345, 12345, 6666, -6666] + , luint16 = [12345, 12345, 6666, 6666] + , lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666] + , luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666] + , lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666] + , luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666] + , linte8 = [123, 12, -66, 66, 34] + , linte16 = [12334, 12, -6666, 6666, 3412] + , linte32 = [12334, 12, -666666666, 6666, 3412] + , linte64 = [12334, 12, -666666666666, 6666, 3412] + , lnum8 = [123, 12.123, -66.456789, 66, 34] + , lnum16 = [12334, -12, -6666.6666, 6666, 3412] + , lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412] + , lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999] + , lnfloat32 = [-666666.88888, 4434.434, 434.43, 3434] + , lnfloat64 = [-666666666666.88888, 4434.434, 434.43, 11111111111.34343, 5566] + , lfloat = [1.1, 2.2, 3.3, 666666.666] + , ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666] + , lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式"/utf8>>] + , lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}] + }, + List = protoMsg:encode(AllType), + iolist_to_binary(List), + %%io:format("~p~n", [List]), + AllType1 = protoMsg:decode(iolist_to_binary(List)). + %%AllType1. + +tall1(0) -> + ok; +tall1(N) -> + AllType = #allType{ + bool = false + , int8 = -66 + , uint8 = 66 + , int16 = -666 + , uint16 = 666 + , int32 = -66666 + , uint32 = 66666 + , int64 = -5294967296 + , uint64 = 5294967296 + , inte8 = -88 + , uinte8 = 88 + , inte16 = -8888 + , uinte16 = 8888 + , inte32 = -888888 + , uinte32 = 888888 + , inte64 = -8888888888 + , uinte64 = 8888888888 + , num8 = -99 + , unum8 = 99 + , num16 = -9999 + , unum16 = 9999 + , num32 = -999999 + , unum32 = 999999 + , num64 = -9999999999 + , unum64 = 9999999999 + , numfloat = 999999.99999 + , numdouble = 9999999999.99999 + , float = 123456.789321 + , double = 4300000000.789321 + , string1 = "this is a test!!!!" + , string2 = "这是一个测试, 等会看结果~!!!" + , lbool = [true, false, true, false] + , lint8 = [123, -123, 66, -66, 88, -88] + , luint8 = [1, 2, 3, 123, 67, 88] + , lint16 = [-12345, 12345, 6666, -6666] + , luint16 = [12345, 12345, 6666, 6666] + , lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666] + , luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666] + , lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666] + , luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666] + , linte8 = [123, 12, -66, 66, 34] + , linte16 = [12334, 12, -6666, 6666, 3412] + , linte32 = [12334, 12, -666666666, 6666, 3412] + , linte64 = [12334, 12, -666666666666, 6666, 3412] + , lnum8 = [123, 12.123, -66.456789, 66, 34] + , lnum16 = [12334, -12, -6666.6666, 6666, 3412] + , lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412] + , lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999] + , lnfloat32 = [-666666.88888, 4434.434, 434.43, 3434] + , lnfloat64 = [-666666666666.88888, 4434.434, 434.43, 11111111111.34343, 5566] + , lfloat = [1.1, 2.2, 3.3, 666666.666] + , ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666] + , lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式"/utf8>>] + , lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}] + }, + %protoMsg:encode(AllType), + term_to_binary(AllType), + tall1(N - 1). + + +tall(N) -> + AllType = #allType{ + bool = false + , int8 = -66 + , uint8 = 66 + , int16 = -666 + , uint16 = 666 + , int32 = -66666 + , uint32 = 66666 + , int64 = -5294967296 + , uint64 = 5294967296 + , inte8 = -88 + , uinte8 = 88 + , inte16 = -8888 + , uinte16 = 8888 + , inte32 = -888888 + , uinte32 = 888888 + , inte64 = -8888888888 + , uinte64 = 8888888888 + , num8 = -99 + , unum8 = 99 + , num16 = -9999 + , unum16 = 9999 + , num32 = -999999 + , unum32 = 999999 + , num64 = -9999999999 + , unum64 = 9999999999 + , numfloat = 999999.99999 + , numdouble = 9999999999.99999 + , float = 123456.789321 + , double = 4300000000.789321 + , string1 = "this is a test!!!!" + , string2 = "这是一个测试, 等会看结果~!!!" + , lbool = [true, false, true, false] + , lint8 = [123, -123, 66, -66, 88, -88] + , luint8 = [1, 2, 3, 123, 67, 88] + , lint16 = [-12345, 12345, 6666, -6666] + , luint16 = [12345, 12345, 6666, 6666] + , lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666] + , luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666] + , lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666] + , luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666] + , linte8 = [123, 12, -66, 66, 34] + , linte16 = [12334, 12, -6666, 6666, 3412] + , linte32 = [12334, 12, -666666666, 6666, 3412] + , linte64 = [12334, 12, -666666666666, 6666, 3412] + , lnum8 = [123, 12.123, -66.456789, 66, 34] + , lnum16 = [12334, -12, -6666.6666, 6666, 3412] + , lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412] + , lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999] + , lnfloat32 = [-666666.88888, 4434.434, 434.43, 3434] + , lnfloat64 = [-666666666666.88888, 4434.434, 434.43, 11111111111.34343, 5566] + , lfloat = [1.1, 2.2, 3.3, 666666.666] + , ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666] + , lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式"/utf8>>] + , lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}] + }, + %List = protoMsg:encode(AllType), + tall(N, term_to_binary(AllType)). + +tall(0, Bin) -> + Bin; +tall(N, Bin) -> + %protoMsg:decode(Bin), + binary_to_term(Bin), + tall(N - 1, Bin). + + +hh1(0) -> + ok; +hh1(N) -> + A = [tt1, tt2, tt3, tt4, {tet, tt1}, {tet, tt2}, {tet, tt3}, yyy], + test(A), + hh1(N - 1). + +hh2(0) -> + ok; +hh2(N) -> + A = [tt1, tt2, tt3, tt4, {tet, tt1}, {tet, tt2}, {tet, tt3}, yyy], + tet(A), + hh2(N - 1). + +test([]) -> + ok; +test([A | T]) -> + case A of + tt1 -> + ok; + tt2 -> + ok; + tt3 -> + ok; + tt4 -> + ok; + {tet, tt1} -> + ok; + {tet, tt2} -> + ok; + {tet, tt3} -> + ok; + _ -> + ok + end, + test(T). + + +tet([]) -> + ok; +tet([tt1 | T]) -> + ok, + tet(T); +tet([tt2 | T]) -> + ok, + tet(T); +tet([tt3 | T]) -> + ok, + tet(T); +tet([tt4 | T]) -> + ok, + tet(T); +tet([{tet, tt1} | T]) -> + ok, + tet(T); +tet([{tet, tt2} | T]) -> + ok, + tet(T); +tet([{tet, tt3} | T]) -> + ok, + tet(T); +tet([YY | T]) -> + ok, + tet(T). + +ttt11(N) -> + Add = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + ttt11(N, Add). + +ttt11(0, Add) -> + ok; +ttt11(N, Add) -> + protoMsg:encode(Add), + ttt11(N - 1, Add). + +%%tt1(Add) -> + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + %%protoMsg:encode(Add). +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%Bin = protoCode:encode(AddressBook), +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%MsgId = protoMsg:getMsgId(element(1, AddressBook)), +%<>. + + +tt(N) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + Bin = protoMsg:encode(AddressBook), + %ok = file:write_file("fff.bin", Bin), + %print_bin(Bin), + tt(N, iolist_to_binary(Bin)). +tt(0, Bin) -> + protoMsg:decode(Bin); +tt(N, Bin) -> + protoMsg:decode(Bin), + tt(N - 1, Bin). + + +%{Len, List, RestBin} = protoMsg("AddressBook", Bin), +%io:format("Len:~p, RestBin:~p~n", [Len, RestBin]), +%io:format("List:~p~n", [List]), +%{Map, _, _} = sproto:decode2("AddressBook", Bin), +%Map. + + +ttt1(0) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "你好啊 嘿嘿"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "范德萨地方范德萨发"}, type = 3} + ] + } + ] + }, + term_to_binary(AddressBook); +ttt1(N) -> + ttt1(), + ttt1(N - 1). + +ttt1() -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "你好啊 嘿嘿"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "范德萨地方范德萨发"}, type = 3} + ] + } + ] + }, + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + Bin = term_to_binary(AddressBook). +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%Bin = protoCode:encode(AddressBook), +%ok = file:write_file("fff.bin", Bin), +%print_bin(Bin), +%MsgId = protoMsg:getMsgId(element(1, AddressBook)), +%<>. + + +ttt(N) -> + AddressBook = #addressBook{ + person = [ + #person{ + name = "Alice", + id = 10000, + phone = [ + #phoneNumber{number = #test{aa = "123456789"}, type = 1}, + #phoneNumber{number = #test{aa = "87654321"}, type = 2} + ] + }, + #person{ + name = "Bob", + id = 20000, + phone = [ + #phoneNumber{number = #test{aa = "01234567890"}, type = 3} + ] + } + ] + }, + %"others" => [ + % #{ + % "name" => "Carol", + % "id" => 30000, + % "phone" => [ + % #{ "number" => #{"aa" => "9876543210"} } + % ] + % } + %] + %AddressBook = #person{name = "1232134", id = 11111,email = "aaa" ,phone = [#phoneNumber{}] }, + %AddressBook = #phoneNumber{number =#test{aa = "dffsaf"},type = 12 }, + %ok = file:write_file("fff.bin", Bin), + %print_bin(Bin), + ttt(N, term_to_binary(AddressBook)). +ttt(0, Bin) -> + binary_to_term(Bin); +ttt(N, Bin) -> + binary_to_term(Bin), + ttt(N - 1, Bin). + +print_bin(Bin) -> + ByteList = lists:reverse(bin_to_hex(Bin, [])), + Fun = fun(Byte, Acc) -> + io:format("~2.16.0b ", [Byte]), + case Acc rem 8 =:= 0 of + true -> io:format("~n", []); + false -> ok + end, + Acc + 1 + end, + lists:foldl(Fun, 1, ByteList), + io:format("bytes:~p~n", [byte_size(Bin)]). + +bin_to_hex(<<>>, Acc) -> + Acc; +bin_to_hex(Bin, Acc) -> + <> = Bin, + bin_to_hex(Bin2, [A | Acc]). + +a1(B) -> + Bin = list_to_binary([X rem 256 || X <- lists:seq(1, 10000)]), + a1(B, Bin). + +a1(0, Bin) -> + <> = Bin, + [X || <> <= ListBin]; +a1(N, Bin) -> + <> = Bin, + A = [X || <> <= ListBin], + B = [X || <> <= ListBin], + io:format("IMY********** ~p~n", [A == B]), + a1(N - 1, Bin). + +a2(B) -> + Bin = list_to_binary([X rem 256 || X <- lists:seq(1, 10000)]), + a2(B, Bin). + +a2(0, Bin) -> + Len = 200, + <> = Bin, + [X || <> <= ListBin]; +a2(N, Bin) -> + Len = 200, + <> = Bin, + [X || <> <= ListBin], + a2(N - 1, Bin). \ No newline at end of file