blob: fcf993a15f46d933305fa8bbcf672a3c29d98e2e [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).
21-export([t/0]).
22
23-include("thriftTest_types.hrl").
24
25test_data() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000026 #xtruct {
27 string_thing = <<"foobar">>,
28 byte_thing = 123,
29 i32_thing = 1234567,
30 i64_thing = 12345678900
31 }.
David Reissbfc57a02009-03-30 20:46:37 +000032
33t1() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000034 {ok, Transport} = thrift_memory_buffer:new(),
35 {ok, Protocol0} = thrift_binary_protocol:new(Transport),
36 TestData = test_data(),
37 {Protocol1, ok} = thrift_protocol:write(Protocol0,
38 {{struct, element(2, thriftTest_types:struct_info('xtruct'))},
39 TestData}),
40 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
41 {struct, element(2, thriftTest_types:struct_info('xtruct'))},
42 'xtruct'),
43 Result = TestData.
David Reissbfc57a02009-03-30 20:46:37 +000044
45
David Reiss233ace52009-03-30 20:46:47 +000046t2() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000047 {ok, Transport} = thrift_memory_buffer:new(),
48 {ok, Protocol0} = thrift_binary_protocol:new(Transport),
49 TestData = test_data(),
50 {Protocol1, ok} = thrift_protocol:write(Protocol0,
51 {{struct, element(2, thriftTest_types:struct_info('xtruct'))},
52 TestData}),
53 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
54 {struct, element(2, thriftTest_types:struct_info('xtruct3'))},
55 'xtruct3'),
David Reiss233ace52009-03-30 20:46:47 +000056
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000057 Result = #xtruct3{string_thing = TestData#xtruct.string_thing,
58 changed = undefined,
59 i32_thing = TestData#xtruct.i32_thing,
60 i64_thing = TestData#xtruct.i64_thing}.
David Reiss233ace52009-03-30 20:46:47 +000061
62
David Reissb3c5f6e2009-03-30 20:46:52 +000063t3() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000064 {ok, Transport} = thrift_memory_buffer:new(),
65 {ok, Protocol0} = thrift_binary_protocol:new(Transport),
66 TestData = #bools{im_true = true, im_false = false},
67 {Protocol1, ok} = thrift_protocol:write(Protocol0,
68 {{struct, element(2, thriftTest_types:struct_info('bools'))},
69 TestData}),
70 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
71 {struct, element(2, thriftTest_types:struct_info('bools'))},
72 'bools'),
David Reissb3c5f6e2009-03-30 20:46:52 +000073
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000074 true = TestData#bools.im_true =:= Result#bools.im_true,
75 true = TestData#bools.im_false =:= Result#bools.im_false.
David Reissb3c5f6e2009-03-30 20:46:52 +000076
77
David Reissb4ab0082010-08-30 22:05:58 +000078t4() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000079 {ok, Transport} = thrift_memory_buffer:new(),
80 {ok, Protocol0} = thrift_binary_protocol:new(Transport),
81 TestData = #insanity{xtructs=[]},
82 {Protocol1, ok} = thrift_protocol:write(Protocol0,
83 {{struct, element(2, thriftTest_types:struct_info('insanity'))},
84 TestData}),
85 {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
86 {struct, element(2, thriftTest_types:struct_info('insanity'))},
87 'insanity'),
David Reissb4ab0082010-08-30 22:05:58 +000088
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000089 TestData = Result.
David Reissb4ab0082010-08-30 22:05:58 +000090
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +000091t5() ->
92 % test writing to a buffer, getting the bytes out, putting them
93 % in a new buffer and reading them
94
95 % here's the writing part
96 {ok, Transport0} = thrift_memory_buffer:new(),
97 {ok, Protocol0} = thrift_binary_protocol:new(Transport0),
98 TestData = test_data(),
99 {Protocol1, ok} = thrift_protocol:write(Protocol0,
100 {{struct, element(2, thriftTest_types:struct_info('xtruct'))},
101 TestData}),
102 % flush now returns the buffer
103 {_Protocol2, Buf} = thrift_protocol:flush_transport (Protocol1),
104
105 % now the reading part
106 {ok, T2} = thrift_memory_buffer:new (Buf),
107 {ok, P2} = thrift_binary_protocol:new(T2),
108 {_, {ok, Result}} = thrift_protocol:read(P2,
109 {struct, element(2, thriftTest_types:struct_info('xtruct'))},
110 'xtruct'),
111
112 Result = TestData.
David Reissb4ab0082010-08-30 22:05:58 +0000113
David Reissbfc57a02009-03-30 20:46:37 +0000114t() ->
Anthony F. Molinaro3d3f4212011-06-18 03:54:04 +0000115 t1(),
116 t2(),
117 t3(),
118 t4(),
119 t5().
David Reissbfc57a02009-03-30 20:46:37 +0000120