]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/erl/test/test_thrift_membuffer_transport.erl
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / erl / test / test_thrift_membuffer_transport.erl
1 %%
2 %% Licensed to the Apache Software Foundation (ASF) under one
3 %% or more contributor license agreements. See the NOTICE file
4 %% distributed with this work for additional information
5 %% regarding copyright ownership. The ASF licenses this file
6 %% to you under the Apache License, Version 2.0 (the
7 %% "License"); you may not use this file except in compliance
8 %% with the License. You may obtain a copy of the License at
9 %%
10 %% http://www.apache.org/licenses/LICENSE-2.0
11 %%
12 %% Unless required by applicable law or agreed to in writing,
13 %% software distributed under the License is distributed on an
14 %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 %% KIND, either express or implied. See the License for the
16 %% specific language governing permissions and limitations
17 %% under the License.
18 %%
19
20 -module(test_thrift_membuffer_transport).
21 -include_lib("eunit/include/eunit.hrl").
22
23
24 new() -> thrift_membuffer_transport:new().
25 new(Data) -> thrift_membuffer_transport:new(Data).
26
27 new_test_() ->
28 [
29 {"new empty membuffer", ?_assertMatch(
30 {ok, {_, _, {t_membuffer, []}}},
31 new()
32 )},
33 {"new membuffer with <<>>", ?_assertMatch(
34 {ok, {_, _, {t_membuffer, [<<>>]}}},
35 new(<<>>)
36 )},
37 {"new membuffer with []", ?_assertMatch(
38 {ok, {_, _, {t_membuffer, []}}},
39 new([])
40 )},
41 {"new membuffer with <<\"hallo world\">>", ?_assertMatch(
42 {ok, {_, _, {t_membuffer, [<<"hallo world">>]}}},
43 new(<<"hallo world">>)
44 )},
45 {"new membuffer with \"hallo world\"", ?_assertMatch(
46 {ok, {_, _, {t_membuffer, "hallo world"}}},
47 new("hallo world")
48 )}
49 ].
50
51
52 read(Membuffer, Bytes) -> thrift_membuffer_transport:read(Membuffer, Bytes).
53
54 read_test_() ->
55 [
56 {"read zero bytes from an empty membuffer", ?_assertMatch(
57 {_, {ok, <<>>}},
58 read({t_membuffer, []}, 0)
59 )},
60 {"read 1 byte from an empty membuffer", ?_assertMatch(
61 {_, {ok, <<>>}},
62 read({t_membuffer, []}, 1)
63 )},
64 {"read zero bytes from nonempty membuffer", ?_assertMatch(
65 {{t_membuffer, <<"hallo world">>}, {ok, <<>>}},
66 read({t_membuffer, [["hallo", " "], "world"]}, 0)
67 )},
68 {"read 1 byte from nonempty membuffer", ?_assertMatch(
69 {{t_membuffer, <<"allo world">>}, {ok, <<"h">>}},
70 read({t_membuffer, [["hallo", " "], "world"]}, 1)
71 )},
72 {"read a zillion bytes from nonempty buffer", ?_assertMatch(
73 {{t_membuffer, <<>>}, {ok, <<"hallo world">>}},
74 read({t_membuffer, [["hallo", " "], "world"]}, 65536)
75 )}
76 ].
77
78
79 read_exact(Membuffer, Bytes) ->
80 thrift_membuffer_transport:read_exact(Membuffer, Bytes).
81
82 read_exact_test_() ->
83 [
84 {"read exactly zero bytes from an empty membuffer", ?_assertMatch(
85 {_, {ok, <<>>}},
86 read_exact({t_membuffer, []}, 0)
87 )},
88 {"read exactly 1 byte from an empty membuffer", ?_assertMatch(
89 {_, {error, eof}},
90 read_exact({t_membuffer, []}, 1)
91 )},
92 {"read exactly zero bytes from nonempty membuffer", ?_assertMatch(
93 {{t_membuffer, <<"hallo world">>}, {ok, <<>>}},
94 read_exact({t_membuffer, [["hallo", " "], "world"]}, 0)
95 )},
96 {"read exactly 1 byte from nonempty membuffer", ?_assertMatch(
97 {{t_membuffer, <<"allo world">>}, {ok, <<"h">>}},
98 read_exact({t_membuffer, [["hallo", " "], "world"]}, 1)
99 )},
100 {"read exactly a zillion bytes from nonempty buffer", ?_assertMatch(
101 {{t_membuffer, [["hallo", " "], "world"]}, {error, eof}},
102 read_exact({t_membuffer, [["hallo", " "], "world"]}, 65536)
103 )}
104 ].
105
106
107 write(Membuffer, Data) -> thrift_membuffer_transport:write(Membuffer, Data).
108
109 write_test_() ->
110 [
111 {"write empty list to empty membuffer", ?_assertMatch(
112 {{t_membuffer, [[], []]}, ok},
113 write({t_membuffer, []}, [])
114 )},
115 {"write empty list to nonempty membuffer", ?_assertMatch(
116 {{t_membuffer, ["hallo world", []]}, ok},
117 write({t_membuffer, "hallo world"}, [])
118 )},
119 {"write empty binary to empty membuffer", ?_assertMatch(
120 {{t_membuffer, [[], <<>>]}, ok},
121 write({t_membuffer, []}, <<>>)
122 )},
123 {"write empty binary to nonempty membuffer", ?_assertMatch(
124 {{t_membuffer, ["hallo world", <<>>]}, ok},
125 write({t_membuffer, "hallo world"}, <<>>)
126 )},
127 {"write a list to empty membuffer", ?_assertMatch(
128 {{t_membuffer, [[], "hallo world"]}, ok},
129 write({t_membuffer, []}, "hallo world")
130 )},
131 {"write a list to nonempty membuffer", ?_assertMatch(
132 {{t_membuffer, [["hallo", " "], "world"]}, ok},
133 write({t_membuffer, ["hallo", " "]}, "world")
134 )},
135 {"write a binary to empty membuffer", ?_assertMatch(
136 {{t_membuffer, [[], <<"hallo world">>]}, ok},
137 write({t_membuffer, []}, <<"hallo world">>)
138 )},
139 {"write a binary to nonempty membuffer", ?_assertMatch(
140 {{t_membuffer, [["hallo", " "], <<"world">>]}, ok},
141 write({t_membuffer, ["hallo", " "]}, <<"world">>)
142 )}
143 ].
144
145
146 flush(Transport) -> thrift_membuffer_transport:flush(Transport).
147
148 flush_test_() ->
149 [
150 {"flush empty membuffer", ?_assertMatch(
151 {{t_membuffer, []}, ok},
152 flush({t_membuffer, []})
153 )},
154 {"flush nonempty membuffer", ?_assertMatch(
155 {{t_membuffer, [<<"hallo world">>]}, ok},
156 flush({t_membuffer, [<<"hallo world">>]})
157 )}
158 ].
159
160
161 close(Transport) -> thrift_membuffer_transport:close(Transport).
162
163 close_test_() ->
164 {"close membuffer", ?_assertMatch(
165 {{t_membuffer, _}, ok},
166 close({t_membuffer, []})
167 )}.