blob: 6ca0b7c521393961db3f87dd45505d38e6ac3976 [file] [log] [blame]
Rich Lane1d5b0102013-05-10 16:28:17 -07001#!/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
29import difflib
30
31# Human-friendly format for binary strings. 8 bytes per line.
32def format_binary(buf):
33 byts = map(ord, buf)
34 lines = [[]]
35 for byt in byts:
36 if len(lines[-1]) == 8:
37 lines.append([])
38 lines[-1].append(byt)
39 return '\n'.join([' '.join(['%02x' % y for y in x]) for x in lines])
40
41def diff(a, b):
42 return '\n'.join(difflib.ndiff(a.splitlines(), b.splitlines()))
43
44# Test serialization / deserialization of a sample object.
45# Depends on the __eq__ method being correct.
46def test_serialization(obj, buf):
47 packed = obj.pack()
48 if packed != buf:
49 a = format_binary(buf)
50 b = format_binary(packed)
51 raise AssertionError("Serialization of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \
52 (type(obj).__name__, a, b, diff(a, b)))
53 unpacked = type(obj).unpack(buf)
54 if obj != unpacked:
55 a = obj.show()
56 b = unpacked.show()
57 raise AssertionError("Deserialization of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \
58 (type(obj).__name__, a, b, diff(a, b)))