Rich Lane | 1d5b010 | 2013-05-10 16:28:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2013, Big Switch Networks, Inc. |
| 3 | # |
| 4 | # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 5 | # the following special exception: |
| 6 | # |
| 7 | # LOXI Exception |
| 8 | # |
| 9 | # As a special exception to the terms of the EPL, you may distribute libraries |
| 10 | # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 11 | # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 12 | # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 13 | # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 14 | # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 15 | # |
| 16 | # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 17 | # |
| 18 | # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 19 | # a copy of the EPL at: |
| 20 | # |
| 21 | # http://www.eclipse.org/legal/epl-v10.html |
| 22 | # |
| 23 | # Unless required by applicable law or agreed to in writing, software |
| 24 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 26 | # EPL for the specific language governing permissions and limitations |
| 27 | # under the EPL. |
| 28 | |
Rich Lane | 9a3f1fd | 2013-05-10 16:29:17 -0700 | [diff] [blame^] | 29 | import sys |
Rich Lane | 1d5b010 | 2013-05-10 16:28:17 -0700 | [diff] [blame] | 30 | import difflib |
Rich Lane | 9a3f1fd | 2013-05-10 16:29:17 -0700 | [diff] [blame^] | 31 | import test_data |
Rich Lane | 1d5b010 | 2013-05-10 16:28:17 -0700 | [diff] [blame] | 32 | |
| 33 | # Human-friendly format for binary strings. 8 bytes per line. |
| 34 | def format_binary(buf): |
| 35 | byts = map(ord, buf) |
| 36 | lines = [[]] |
| 37 | for byt in byts: |
| 38 | if len(lines[-1]) == 8: |
| 39 | lines.append([]) |
| 40 | lines[-1].append(byt) |
| 41 | return '\n'.join([' '.join(['%02x' % y for y in x]) for x in lines]) |
| 42 | |
| 43 | def diff(a, b): |
| 44 | return '\n'.join(difflib.ndiff(a.splitlines(), b.splitlines())) |
| 45 | |
| 46 | # Test serialization / deserialization of a sample object. |
| 47 | # Depends on the __eq__ method being correct. |
| 48 | def test_serialization(obj, buf): |
| 49 | packed = obj.pack() |
| 50 | if packed != buf: |
| 51 | a = format_binary(buf) |
| 52 | b = format_binary(packed) |
| 53 | raise AssertionError("Serialization of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \ |
| 54 | (type(obj).__name__, a, b, diff(a, b))) |
| 55 | unpacked = type(obj).unpack(buf) |
| 56 | if obj != unpacked: |
| 57 | a = obj.show() |
| 58 | b = unpacked.show() |
| 59 | raise AssertionError("Deserialization of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \ |
| 60 | (type(obj).__name__, a, b, diff(a, b))) |
Rich Lane | 9a3f1fd | 2013-05-10 16:29:17 -0700 | [diff] [blame^] | 61 | |
| 62 | def test_pretty(obj, expected): |
| 63 | pretty = obj.show() |
| 64 | if expected != pretty: |
| 65 | raise AssertionError("Pretty printing of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \ |
| 66 | (type(obj).__name__, expected, pretty, diff(expected, pretty))) |
| 67 | |
| 68 | # Run test_serialization and possibly test_pretty against the named data file |
| 69 | # Uses the globals of the calling function to get 'ofp' |
| 70 | def test_datafile(name): |
| 71 | data = test_data.read(name) |
| 72 | binary = data['binary'] |
| 73 | python = data['python'] |
| 74 | obj = eval(python, sys._getframe(1).f_globals) |
| 75 | test_serialization(obj, binary) |
| 76 | if 'python pretty-printer' in data: |
| 77 | test_pretty(obj, data['python pretty-printer']) |