backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / lib / test_pack_utils.py
1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import unittest
17 import six
18 import struct
19
20 from nose.tools import ok_, eq_
21
22 from ryu.lib import pack_utils
23
24
25 class TestMsgPackInto(unittest.TestCase):
26     """ Test case for msg_pack_into
27     """
28
29     def _test_msg_pack_into(self, offset_type='e'):
30         fmt = '!HH'
31         len_ = struct.calcsize(fmt)
32         buf = bytearray(len_)
33         offset = len_
34         arg1 = 1
35         arg2 = 2
36
37         if offset_type == 'l':
38             offset += 1
39         elif offset_type == 'g':
40             offset -= 1
41
42         pack_utils.msg_pack_into(fmt, buf, offset, arg1, arg2)
43
44         check_offset = len(buf) - len_
45         res = struct.unpack_from(fmt, six.binary_type(buf), check_offset)
46
47         eq_(arg1, res[0])
48         eq_(arg2, res[1])
49
50         return True
51
52     def test_msg_pack_into(self):
53         ok_(self._test_msg_pack_into())
54
55     def test_msg_pack_into_less(self):
56         ok_(self._test_msg_pack_into('l'))
57
58     def test_msg_pack_into_greater(self):
59         ok_(self._test_msg_pack_into('g'))