blob: 671ae1100e42855c129b8c41915b6c19fe65f535 [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
Jens Geyer6d15c302014-10-02 10:03:09 +020025-include("gen-erl/thrift_test_types.hrl").
David Reissbfc57a02009-03-30 20:46:37 +000026
27test_data() ->
Jens Geyer6d15c302014-10-02 10:03:09 +020028 #'Xtruct'{
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000029 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,
Jens Geyer6d15c302014-10-02 10:03:09 +020040 {{struct, element(2, thrift_test_types:struct_info('Xtruct'))},
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000041 TestData}),
42 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
Jens Geyer6d15c302014-10-02 10:03:09 +020043 {struct, element(2, thrift_test_types:struct_info('Xtruct'))},
44 'Xtruct'),
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000045 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,
Jens Geyer6d15c302014-10-02 10:03:09 +020052 {{struct, element(2, thrift_test_types:struct_info('Xtruct'))},
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000053 TestData}),
54 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
Jens Geyer6d15c302014-10-02 10:03:09 +020055 {struct, element(2, thrift_test_types:struct_info('Xtruct3'))},
56 'Xtruct3'),
David Reiss233ace52009-03-30 20:46:47 +000057
Jens Geyer6d15c302014-10-02 10:03:09 +020058 Result = #'Xtruct3'{string_thing = TestData#'Xtruct'.string_thing,
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000059 changed = undefined,
Jens Geyer6d15c302014-10-02 10:03:09 +020060 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),
Jens Geyer6d15c302014-10-02 10:03:09 +020067 TestData = #'Bools'{im_true = true, im_false = false},
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000068 {Protocol1, ok} = thrift_protocol:write(Protocol0,
Jens Geyer6d15c302014-10-02 10:03:09 +020069 {{struct, element(2, thrift_test_types:struct_info('Bools'))},
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000070 TestData}),
71 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
Jens Geyer6d15c302014-10-02 10:03:09 +020072 {struct, element(2, thrift_test_types:struct_info('Bools'))},
73 'Bools'),
David Reissb3c5f6e2009-03-30 20:46:52 +000074
Jens Geyer6d15c302014-10-02 10:03:09 +020075 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),
Jens Geyer6d15c302014-10-02 10:03:09 +020082 TestData = #'Insanity'{xtructs=[]},
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000083 {Protocol1, ok} = thrift_protocol:write(Protocol0,
Jens Geyer6d15c302014-10-02 10:03:09 +020084 {{struct, element(2, thrift_test_types:struct_info('Insanity'))},
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000085 TestData}),
86 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
Jens Geyer6d15c302014-10-02 10:03:09 +020087 {struct, element(2, thrift_test_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,
Jens Geyer6d15c302014-10-02 10:03:09 +0200101 {{struct, element(2, thrift_test_types:struct_info('Xtruct'))},
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +0000102 TestData}),
103 % flush now returns the buffer
Jens Geyer6d15c302014-10-02 10:03:09 +0200104 {_Protocol2, Buf} = thrift_protocol:flush_transport(Protocol1),
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +0000105
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,
Jens Geyer6d15c302014-10-02 10:03:09 +0200110 {struct, element(2, thrift_test_types:struct_info('Xtruct'))},
111 'Xtruct'),
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +0000112
113 Result = TestData.
David Reissb4ab0082010-08-30 22:05:58 +0000114
Anthony F. Molinaro917d8982011-06-21 06:20:18 +0000115-endif.