blob: a293283c8e69a07ced154ea0bdb03e05b6595c02 [file] [log] [blame]
Marc-André Lemburg92b201d2005-10-21 13:47:03 +00001#!/usr/bin/env python
2
3""" Compare the output of two codecs.
4
5(c) Copyright 2005, Marc-Andre Lemburg (mal@lemburg.com).
6
7 Licensed to PSF under a Contributor Agreement.
8
9"""
10import sys
11
12def compare_codecs(encoding1, encoding2):
13
14 print 'Comparing encoding/decoding of %r and %r' % (encoding1, encoding2)
15 mismatch = 0
16 # Check encoding
17 for i in range(sys.maxunicode):
18 u = unichr(i)
19 try:
20 c1 = u.encode(encoding1)
Guido van Rossumb940e112007-01-10 16:19:56 +000021 except UnicodeError as reason:
Marc-André Lemburg92b201d2005-10-21 13:47:03 +000022 c1 = '<undefined>'
23 try:
24 c2 = u.encode(encoding2)
Guido van Rossumb940e112007-01-10 16:19:56 +000025 except UnicodeError as reason:
Marc-André Lemburg92b201d2005-10-21 13:47:03 +000026 c2 = '<undefined>'
27 if c1 != c2:
28 print ' * encoding mismatch for 0x%04X: %-14r != %r' % \
29 (i, c1, c2)
30 mismatch += 1
31 # Check decoding
32 for i in range(256):
33 c = chr(i)
34 try:
35 u1 = c.decode(encoding1)
36 except UnicodeError:
37 u1 = u'<undefined>'
38 try:
39 u2 = c.decode(encoding2)
40 except UnicodeError:
41 u2 = u'<undefined>'
42 if u1 != u2:
43 print ' * decoding mismatch for 0x%04X: %-14r != %r' % \
44 (i, u1, u2)
45 mismatch += 1
46 if mismatch:
47 print
48 print 'Found %i mismatches' % mismatch
49 else:
50 print '-> Codecs are identical.'
51
52if __name__ == '__main__':
53 compare_codecs(sys.argv[1], sys.argv[2])