David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 20 | -module(test_membuffer). |
| 21 | -export([t/0]). |
| 22 | |
| 23 | -include("thriftTest_types.hrl"). |
| 24 | |
| 25 | test_data() -> |
| 26 | #xtruct{string_thing = <<"foobar">>, |
| 27 | byte_thing = 123, |
| 28 | i32_thing = 1234567, |
| 29 | i64_thing = 12345678900}. |
| 30 | |
| 31 | t1() -> |
| 32 | {ok, Transport} = thrift_memory_buffer:new(), |
David Reiss | f28f23d | 2010-08-30 22:05:48 +0000 | [diff] [blame] | 33 | {ok, Protocol0} = thrift_binary_protocol:new(Transport), |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 34 | TestData = test_data(), |
David Reiss | f28f23d | 2010-08-30 22:05:48 +0000 | [diff] [blame] | 35 | {Protocol1, ok} = thrift_protocol:write(Protocol0, |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 36 | {{struct, element(2, thriftTest_types:struct_info('xtruct'))}, |
| 37 | TestData}), |
David Reiss | f28f23d | 2010-08-30 22:05:48 +0000 | [diff] [blame] | 38 | {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1, |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 39 | {struct, element(2, thriftTest_types:struct_info('xtruct'))}, |
| 40 | 'xtruct'), |
| 41 | |
| 42 | Result = TestData. |
| 43 | |
| 44 | |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 45 | t2() -> |
| 46 | {ok, Transport} = thrift_memory_buffer:new(), |
David Reiss | f28f23d | 2010-08-30 22:05:48 +0000 | [diff] [blame] | 47 | {ok, Protocol0} = thrift_binary_protocol:new(Transport), |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 48 | TestData = test_data(), |
David Reiss | f28f23d | 2010-08-30 22:05:48 +0000 | [diff] [blame] | 49 | {Protocol1, ok} = thrift_protocol:write(Protocol0, |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 50 | {{struct, element(2, thriftTest_types:struct_info('xtruct'))}, |
| 51 | TestData}), |
David Reiss | f28f23d | 2010-08-30 22:05:48 +0000 | [diff] [blame] | 52 | {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1, |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 53 | {struct, element(2, thriftTest_types:struct_info('xtruct3'))}, |
| 54 | 'xtruct3'), |
| 55 | |
| 56 | Result = #xtruct3{string_thing = TestData#xtruct.string_thing, |
| 57 | changed = undefined, |
| 58 | i32_thing = TestData#xtruct.i32_thing, |
| 59 | i64_thing = TestData#xtruct.i64_thing}. |
| 60 | |
| 61 | |
David Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 62 | t3() -> |
| 63 | {ok, Transport} = thrift_memory_buffer:new(), |
David Reiss | f28f23d | 2010-08-30 22:05:48 +0000 | [diff] [blame] | 64 | {ok, Protocol0} = thrift_binary_protocol:new(Transport), |
David Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 65 | TestData = #bools{im_true = true, im_false = false}, |
David Reiss | f28f23d | 2010-08-30 22:05:48 +0000 | [diff] [blame] | 66 | {Protocol1, ok} = thrift_protocol:write(Protocol0, |
David Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 67 | {{struct, element(2, thriftTest_types:struct_info('bools'))}, |
| 68 | TestData}), |
David Reiss | f28f23d | 2010-08-30 22:05:48 +0000 | [diff] [blame] | 69 | {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1, |
David Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 70 | {struct, element(2, thriftTest_types:struct_info('bools'))}, |
| 71 | 'bools'), |
| 72 | |
| 73 | true = TestData#bools.im_true =:= Result#bools.im_true, |
| 74 | true = TestData#bools.im_false =:= Result#bools.im_false. |
| 75 | |
| 76 | |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 77 | t4() -> |
| 78 | {ok, Transport} = thrift_memory_buffer:new(), |
| 79 | {ok, Protocol0} = thrift_binary_protocol:new(Transport), |
| 80 | TestData = #insanity{xtructs=[]}, |
| 81 | {Protocol1, ok} = thrift_protocol:write(Protocol0, |
| 82 | {{struct, element(2, thriftTest_types:struct_info('insanity'))}, |
| 83 | TestData}), |
| 84 | {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1, |
| 85 | {struct, element(2, thriftTest_types:struct_info('insanity'))}, |
| 86 | 'insanity'), |
| 87 | |
| 88 | TestData = Result. |
| 89 | |
| 90 | |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 91 | t() -> |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 92 | t1(), |
David Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 93 | t2(), |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 94 | t3(), |
| 95 | t4(). |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 96 | |