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). |
Anthony F. Molinaro | 917d898 | 2011-06-21 06:20:18 +0000 | [diff] [blame] | 21 | |
| 22 | -ifdef(TEST). |
| 23 | -include_lib("eunit/include/eunit.hrl"). |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 24 | |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 25 | -include("gen-erl/thrift_test_types.hrl"). |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 26 | |
| 27 | test_data() -> |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 28 | #'Xtruct'{ |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 29 | string_thing = <<"foobar">>, |
| 30 | byte_thing = 123, |
| 31 | i32_thing = 1234567, |
| 32 | i64_thing = 12345678900 |
| 33 | }. |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 34 | |
Anthony F. Molinaro | 917d898 | 2011-06-21 06:20:18 +0000 | [diff] [blame] | 35 | encode_decode_1_test() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 36 | {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 Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 40 | {{struct, element(2, thrift_test_types:struct_info('Xtruct'))}, |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 41 | TestData}), |
| 42 | {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1, |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 43 | {struct, element(2, thrift_test_types:struct_info('Xtruct'))}, |
| 44 | 'Xtruct'), |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 45 | Result = TestData. |
David Reiss | bfc57a0 | 2009-03-30 20:46:37 +0000 | [diff] [blame] | 46 | |
Anthony F. Molinaro | 917d898 | 2011-06-21 06:20:18 +0000 | [diff] [blame] | 47 | encode_decode_2_test() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 48 | {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 Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 52 | {{struct, element(2, thrift_test_types:struct_info('Xtruct'))}, |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 53 | TestData}), |
| 54 | {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1, |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 55 | {struct, element(2, thrift_test_types:struct_info('Xtruct3'))}, |
| 56 | 'Xtruct3'), |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 57 | |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 58 | Result = #'Xtruct3'{string_thing = TestData#'Xtruct'.string_thing, |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 59 | changed = undefined, |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 60 | i32_thing = TestData#'Xtruct'.i32_thing, |
| 61 | i64_thing = TestData#'Xtruct'.i64_thing}. |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 62 | |
| 63 | |
Anthony F. Molinaro | 917d898 | 2011-06-21 06:20:18 +0000 | [diff] [blame] | 64 | encode_decode_3_test() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 65 | {ok, Transport} = thrift_memory_buffer:new(), |
| 66 | {ok, Protocol0} = thrift_binary_protocol:new(Transport), |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 67 | TestData = #'Bools'{im_true = true, im_false = false}, |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 68 | {Protocol1, ok} = thrift_protocol:write(Protocol0, |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 69 | {{struct, element(2, thrift_test_types:struct_info('Bools'))}, |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 70 | TestData}), |
| 71 | {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1, |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 72 | {struct, element(2, thrift_test_types:struct_info('Bools'))}, |
| 73 | 'Bools'), |
David Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 74 | |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 75 | true = TestData#'Bools'.im_true =:= Result#'Bools'.im_true, |
| 76 | true = TestData#'Bools'.im_false =:= Result#'Bools'.im_false. |
David Reiss | b3c5f6e | 2009-03-30 20:46:52 +0000 | [diff] [blame] | 77 | |
| 78 | |
Anthony F. Molinaro | 917d898 | 2011-06-21 06:20:18 +0000 | [diff] [blame] | 79 | encode_decode_4_test() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 80 | {ok, Transport} = thrift_memory_buffer:new(), |
| 81 | {ok, Protocol0} = thrift_binary_protocol:new(Transport), |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 82 | TestData = #'Insanity'{xtructs=[]}, |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 83 | {Protocol1, ok} = thrift_protocol:write(Protocol0, |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 84 | {{struct, element(2, thrift_test_types:struct_info('Insanity'))}, |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 85 | TestData}), |
| 86 | {_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1, |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 87 | {struct, element(2, thrift_test_types:struct_info('Insanity'))}, |
| 88 | 'Insanity'), |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 89 | |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 90 | TestData = Result. |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 91 | |
Anthony F. Molinaro | 917d898 | 2011-06-21 06:20:18 +0000 | [diff] [blame] | 92 | encode_decode_5_test() -> |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 93 | % 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 Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 101 | {{struct, element(2, thrift_test_types:struct_info('Xtruct'))}, |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 102 | TestData}), |
| 103 | % flush now returns the buffer |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 104 | {_Protocol2, Buf} = thrift_protocol:flush_transport(Protocol1), |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 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, |
Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame^] | 110 | {struct, element(2, thrift_test_types:struct_info('Xtruct'))}, |
| 111 | 'Xtruct'), |
Anthony F. Molinaro | 3d3f421 | 2011-06-18 03:54:04 +0000 | [diff] [blame] | 112 | |
| 113 | Result = TestData. |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 114 | |
Anthony F. Molinaro | 917d898 | 2011-06-21 06:20:18 +0000 | [diff] [blame] | 115 | -endif. |