Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 1 | -- Licensed to the Apache Software Foundation (ASF) under one |
| 2 | -- or more contributor license agreements. See the NOTICE file |
| 3 | -- distributed with this work for additional information |
| 4 | -- regarding copyright ownership. The ASF licenses this file |
| 5 | -- to you under the Apache License, Version 2.0 (the |
| 6 | -- "License"); you may not use this file except in compliance |
| 7 | -- with the License. You may obtain a copy of the License at |
| 8 | |
| 9 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | -- Unless required by applicable law or agreed to in writing, |
| 12 | -- software distributed under the License is distributed on an |
| 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | -- KIND, either express or implied. See the License for the |
| 15 | -- specific language governing permissions and limitations |
| 16 | -- under the License. |
| 17 | |
| 18 | |
| 19 | require('TSocket') |
| 20 | require('TBinaryProtocol') |
| 21 | require('ThriftTest_ThriftTest') |
| 22 | require('liblualongnumber') |
| 23 | |
| 24 | local client |
| 25 | |
| 26 | function teardown() |
| 27 | if client then |
| 28 | -- Shuts down the server |
| 29 | client:testVoid() |
| 30 | |
| 31 | -- close the connection |
| 32 | client:close() |
| 33 | end |
| 34 | end |
| 35 | |
| 36 | function assertEqual(val1, val2, msg) |
| 37 | assert(val1 == val2, msg) |
| 38 | end |
| 39 | |
| 40 | function testBasicClient() |
| 41 | local socket = TSocket:new{ |
| 42 | port = 9090 |
| 43 | } |
| 44 | assert(socket, 'Failed to create client socket') |
| 45 | socket:setTimeout(5000) |
| 46 | |
| 47 | local protocol = TBinaryProtocol:new{ |
| 48 | trans = socket |
| 49 | } |
| 50 | assert(protocol, 'Failed to create binary protocol') |
| 51 | |
| 52 | client = ThriftTestClient:new{ |
| 53 | protocol = protocol |
| 54 | } |
| 55 | assert(client, 'Failed to create client') |
| 56 | |
| 57 | -- Open the socket |
| 58 | local status, _ = pcall(socket.open, socket) |
| 59 | assert(status, 'Failed to connect to server') |
| 60 | |
| 61 | -- String |
| 62 | assertEqual(client:testString('lala'), 'lala', 'Failed testString') |
| 63 | assertEqual(client:testString('wahoo'), 'wahoo', 'Failed testString') |
| 64 | |
| 65 | -- Byte |
| 66 | assertEqual(client:testByte(0x01), 1, 'Failed testByte 1') |
| 67 | assertEqual(client:testByte(0x40), 64, 'Failed testByte 2') |
| 68 | assertEqual(client:testByte(0x7f), 127, 'Failed testByte 3') |
| 69 | assertEqual(client:testByte(0x80), -128, 'Failed testByte 4') |
| 70 | assertEqual(client:testByte(0xbf), -65, 'Failed testByte 5') |
| 71 | assertEqual(client:testByte(0xff), -1, 'Failed testByte 6') |
| 72 | assertEqual(client:testByte(128), -128, 'Failed testByte 7') |
| 73 | assertEqual(client:testByte(255), -1, 'Failed testByte 8') |
| 74 | |
| 75 | -- I32 |
| 76 | assertEqual(client:testI32(0x00000001), 1, 'Failed testI32 1') |
| 77 | assertEqual(client:testI32(0x40000000), 1073741824, 'Failed testI32 2') |
| 78 | assertEqual(client:testI32(0x7fffffff), 2147483647, 'Failed testI32 3') |
| 79 | assertEqual(client:testI32(0x80000000), -2147483648, 'Failed testI32 4') |
| 80 | assertEqual(client:testI32(0xbfffffff), -1073741825, 'Failed testI32 5') |
| 81 | assertEqual(client:testI32(0xffffffff), -1, 'Failed testI32 6') |
| 82 | assertEqual(client:testI32(2147483648), -2147483648, 'Failed testI32 7') |
| 83 | assertEqual(client:testI32(4294967295), -1, 'Failed testI32 8') |
| 84 | |
| 85 | -- I64 (lua only supports 16 decimal precision so larger numbers are |
| 86 | -- initialized by their string value) |
| 87 | local long = liblualongnumber.new |
| 88 | assertEqual(client:testI64(long(0x0000000000000001)), |
| 89 | long(1), |
| 90 | 'Failed testI64 1') |
| 91 | assertEqual(client:testI64(long(0x4000000000000000)), |
| 92 | long(4611686018427387904), |
| 93 | 'Failed testI64 2') |
| 94 | assertEqual(client:testI64(long('0x7fffffffffffffff')), |
| 95 | long('9223372036854775807'), |
| 96 | 'Failed testI64 3') |
| 97 | assertEqual(client:testI64(long(0x8000000000000000)), |
| 98 | long(-9223372036854775808), |
| 99 | 'Failed testI64 4') |
| 100 | assertEqual(client:testI64(long('0xbfffffffffffffff')), |
| 101 | long('-4611686018427387905'), |
| 102 | 'Failed testI64 5') |
| 103 | assertEqual(client:testI64(long('0xffffffffffffffff')), |
| 104 | long(-1), |
| 105 | 'Failed testI64 6') |
| 106 | |
| 107 | -- Double |
| 108 | assertEqual( |
| 109 | client:testDouble(1.23456789), 1.23456789, 'Failed testDouble 1') |
| 110 | assertEqual( |
| 111 | client:testDouble(0.123456789), 0.123456789, 'Failed testDouble 2') |
| 112 | assertEqual( |
| 113 | client:testDouble(0.123456789), 0.123456789, 'Failed testDouble 3') |
| 114 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 115 | -- TODO testBinary() ... |
| 116 | |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 117 | -- Accuracy of 16 decimal digits (rounds) |
| 118 | local a, b = 1.12345678906666663, 1.12345678906666661 |
| 119 | assertEqual(a, b) |
| 120 | assertEqual(client:testDouble(a), b, 'Failed testDouble 5') |
| 121 | |
| 122 | -- Struct |
| 123 | local a = { |
| 124 | string_thing = 'Zero', |
| 125 | byte_thing = 1, |
| 126 | i32_thing = -3, |
| 127 | i64_thing = long(-5) |
| 128 | } |
| 129 | |
| 130 | -- TODO fix client struct equality |
| 131 | --assertEqual(client:testStruct(a), a, 'Failed testStruct') |
| 132 | |
| 133 | -- Call the void function and end the test (handler stops server) |
| 134 | client:testVoid() |
| 135 | end |
| 136 | |
| 137 | testBasicClient() |
| 138 | teardown() |