blob: 7a011435d8f590180bba45202c537dfc7f180948 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
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
David Reissbfc57a02009-03-30 20:46:37 +000020-module(test_membuffer).
Anthony F. Molinaro917d8982011-06-21 06:20:18 +000021
22-ifdef(TEST).
23-include_lib("eunit/include/eunit.hrl").
David Reissbfc57a02009-03-30 20:46:37 +000024
25-include("thriftTest_types.hrl").
26
27test_data() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000028 #xtruct {
29 string_thing = <<"foobar">>,
30 byte_thing = 123,
31 i32_thing = 1234567,
32 i64_thing = 12345678900
33 }.
David Reissbfc57a02009-03-30 20:46:37 +000034
Anthony F. Molinaro917d8982011-06-21 06:20:18 +000035encode_decode_1_test() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000036 {ok, Transport} = thrift_memory_buffer:new(),
37 {ok, Protocol0} = thrift_binary_protocol:new(Transport),
38 TestData = test_data(),
39 {Protocol1, ok} = thrift_protocol:write(Protocol0,
40 {{struct, element(2, thriftTest_types:struct_info('xtruct'))},
41 TestData}),
42 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
43 {struct, element(2, thriftTest_types:struct_info('xtruct'))},
44 'xtruct'),
45 Result = TestData.
David Reissbfc57a02009-03-30 20:46:37 +000046
Anthony F. Molinaro917d8982011-06-21 06:20:18 +000047encode_decode_2_test() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000048 {ok, Transport} = thrift_memory_buffer:new(),
49 {ok, Protocol0} = thrift_binary_protocol:new(Transport),
50 TestData = test_data(),
51 {Protocol1, ok} = thrift_protocol:write(Protocol0,
52 {{struct, element(2, thriftTest_types:struct_info('xtruct'))},
53 TestData}),
54 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
55 {struct, element(2, thriftTest_types:struct_info('xtruct3'))},
56 'xtruct3'),
David Reiss233ace52009-03-30 20:46:47 +000057
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000058 Result = #xtruct3{string_thing = TestData#xtruct.string_thing,
59 changed = undefined,
60 i32_thing = TestData#xtruct.i32_thing,
61 i64_thing = TestData#xtruct.i64_thing}.
David Reiss233ace52009-03-30 20:46:47 +000062
63
Anthony F. Molinaro917d8982011-06-21 06:20:18 +000064encode_decode_3_test() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000065 {ok, Transport} = thrift_memory_buffer:new(),
66 {ok, Protocol0} = thrift_binary_protocol:new(Transport),
67 TestData = #bools{im_true = true, im_false = false},
68 {Protocol1, ok} = thrift_protocol:write(Protocol0,
69 {{struct, element(2, thriftTest_types:struct_info('bools'))},
70 TestData}),
71 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
72 {struct, element(2, thriftTest_types:struct_info('bools'))},
73 'bools'),
David Reissb3c5f6e2009-03-30 20:46:52 +000074
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000075 true = TestData#bools.im_true =:= Result#bools.im_true,
76 true = TestData#bools.im_false =:= Result#bools.im_false.
David Reissb3c5f6e2009-03-30 20:46:52 +000077
78
Anthony F. Molinaro917d8982011-06-21 06:20:18 +000079encode_decode_4_test() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000080 {ok, Transport} = thrift_memory_buffer:new(),
81 {ok, Protocol0} = thrift_binary_protocol:new(Transport),
82 TestData = #insanity{xtructs=[]},
83 {Protocol1, ok} = thrift_protocol:write(Protocol0,
84 {{struct, element(2, thriftTest_types:struct_info('insanity'))},
85 TestData}),
86 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
87 {struct, element(2, thriftTest_types:struct_info('insanity'))},
88 'insanity'),
David Reissb4ab0082010-08-30 22:05:58 +000089
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000090 TestData = Result.
David Reissb4ab0082010-08-30 22:05:58 +000091
Anthony F. Molinaro917d8982011-06-21 06:20:18 +000092encode_decode_5_test() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000093 % test writing to a buffer, getting the bytes out, putting them
94 % in a new buffer and reading them
95
96 % here's the writing part
97 {ok, Transport0} = thrift_memory_buffer:new(),
98 {ok, Protocol0} = thrift_binary_protocol:new(Transport0),
99 TestData = test_data(),
100 {Protocol1, ok} = thrift_protocol:write(Protocol0,
101 {{struct, element(2, thriftTest_types:struct_info('xtruct'))},
102 TestData}),
103 % flush now returns the buffer
104 {_Protocol2, Buf} = thrift_protocol:flush_transport (Protocol1),
105
106 % now the reading part
107 {ok, T2} = thrift_memory_buffer:new (Buf),
108 {ok, P2} = thrift_binary_protocol:new(T2),
109 {_, {ok, Result}} = thrift_protocol:read(P2,
110 {struct, element(2, thriftTest_types:struct_info('xtruct'))},
111 'xtruct'),
112
113 Result = TestData.
David Reissb4ab0082010-08-30 22:05:58 +0000114
Anthony F. Molinaro917d8982011-06-21 06:20:18 +0000115-endif.