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() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 26 | #xtruct { |
| 27 | string_thing = <<"foobar">>, |
| 28 | byte_thing = 123, |
| 29 | i32_thing = 1234567, |
| 30 | i64_thing = 12345678900 |
| 31 | }. |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 32 | |
| 33 | t1() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 34 | {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 Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 44 | |
| 45 | |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 46 | t2() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 47 | {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 Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 56 | |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 57 | 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 Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 61 | |
| 62 | |
David Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 63 | t3() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 64 | {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 Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 73 | |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 74 | true = TestData#bools.im_true =:= Result#bools.im_true, |
| 75 | true = TestData#bools.im_false =:= Result#bools.im_false. |
David Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 76 | |
| 77 | |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 78 | t4() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 79 | {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 Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 88 | |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 89 | TestData = Result. |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 90 | |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 91 | t5() -> |
| 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 Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 113 | |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 114 | t() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame^] | 115 | t1(), |
| 116 | t2(), |
| 117 | t3(), |
| 118 | t4(), |
| 119 | t5(). |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 120 | |