Пример #1
0
    def test_find_optimal_sequence_non_ASCII_strings(self):
        """ Test find_optimal_sequence function for non-ASCII strings"""

        # Implicit iso8559-1 without ECI:
        self.assertEqual(find_optimal_sequence('Français'), b(
            'F', Latch.LOWER, 'r', 'a', 'n', Shift.BINARY, 1, 0xe7, 'a', 'i', 's'))

        # ECI: explicit iso8859-1, cp1252 (Windows-1252), and utf-8
        self.assertEqual(find_optimal_sequence('Français', 'iso8859-1'), self._optimal_eci_sequence('iso8859-1') + b(
            'F', Latch.LOWER, 'r', 'a', 'n', Shift.BINARY, 1, 0xe7, 'a', 'i', 's'))
        self.assertEqual(find_optimal_sequence('€800', 'cp1252'), self._optimal_eci_sequence('cp1252') + b(
            Shift.BINARY, 1, 0x80, Latch.DIGIT, '8', '0', '0'))
        self.assertEqual(find_optimal_sequence('Français', 'utf-8'), self._optimal_eci_sequence('utf-8') + b(
            'F', Latch.LOWER, 'r', 'a', 'n', Shift.BINARY, 2, 0xc3, 0xa7, 'a', 'i', 's'))
Пример #2
0
    def test_find_optimal_sequence_bytes(self):
        """ Test find_optimal_sequence function for byte strings """

        self.assertEqual(find_optimal_sequence(b'a' + b'\xff' * 31 + b'A'), b(
            Shift.BINARY, 0, 1, 'a') + [0xff] * 31 + b('A'))
        self.assertEqual(find_optimal_sequence(b'abc' + b'\xff' * 32 + b'A'), b(
            Latch.LOWER, 'a', 'b', 'c', Shift.BINARY, 0, 1) + [0xff] * 32 + b(Latch.DIGIT, Latch.UPPER, 'A'))
        self.assertEqual(find_optimal_sequence(b'abc' + b'\xff' * 31 + b'@\\\\'), b(
            Latch.LOWER, 'a', 'b', 'c', Shift.BINARY, 31) + [0xff] * 31 + b(Latch.MIXED, '@', '\\', '\\'))
        self.assertEqual(find_optimal_sequence(b'!#$%&?\xff'), b(
            Latch.MIXED, Latch.PUNCT, '!', '#', '$', '%', '&', '?', Latch.UPPER, Shift.BINARY, 1, '\xff'))
        self.assertEqual(find_optimal_sequence(b'!#$%&\xff'), b(Shift.BINARY, 6, '!', '#', '$', '%', '&', '\xff'))
        self.assertEqual(find_optimal_sequence(b'@\xff'), b(Shift.BINARY, 2, '@', '\xff'))
        self.assertEqual(find_optimal_sequence(b'. @\xff'), b(Shift.PUNCT, '. ', Shift.BINARY, 2, '@', '\xff'))
 def test_find_optimal_sequence(self):
     """ Test find_optimal_sequence function """
     self.assertEqual(find_optimal_sequence(''), [])
     self.assertEqual(find_optimal_sequence('ABC'), ['A', 'B', 'C'])
     self.assertEqual(find_optimal_sequence('abc'), ['L/L', 'a', 'b', 'c'])
     self.assertEqual(find_optimal_sequence('Wikipedia, the free encyclopedia'), [
         'W', 'L/L', 'i', 'k', 'i', 'p', 'e', 'd', 'i', 'a', 'P/S', ', ', 't', 'h', 'e',
         ' ', 'f', 'r', 'e', 'e', ' ', 'e', 'n', 'c', 'y', 'c', 'l', 'o', 'p', 'e', 'd', 'i', 'a'])
     self.assertEqual(find_optimal_sequence('Code 2D!'), [
         'C', 'L/L', 'o', 'd', 'e', 'D/L', ' ', '2', 'U/S', 'D', 'P/S', '!'])
     self.assertEqual(find_optimal_sequence('a\xff'), ['B/S', 2, 'a', '\xff'])
     self.assertEqual(find_optimal_sequence('a' + '\xff' * 30), ['B/S', 31, 'a'] + ['\xff'] * 30)
     self.assertEqual(find_optimal_sequence('a' + '\xff' * 31), ['B/S', 0, 32, 'a'] + ['\xff'] * 31)
     self.assertEqual(find_optimal_sequence('!#$%&?'), ['M/L', 'P/L', '!', '#', '$', '%', '&', '?'])
     self.assertEqual(find_optimal_sequence('!#$%&?\xff'), [
         'M/L', 'P/L', '!', '#', '$', '%', '&', '?', 'U/L', 'B/S', 1, '\xff'])
     self.assertEqual(find_optimal_sequence('!#$%&\xff'), ['B/S', 6, '!', '#', '$', '%', '&', '\xff'])
     self.assertEqual(find_optimal_sequence('@\xff'), ['B/S', 2, '@', '\xff'])
     self.assertEqual(find_optimal_sequence('. @\xff'), ['P/S', '. ', 'B/S', 2, '@', '\xff'])
     self.assertIn(find_optimal_sequence('. : '), [['P/S', '. ', 'P/S', ': '], ['M/L', 'P/L', '. ', ': ']])
     self.assertEqual(find_optimal_sequence('\r\n\r\n\r\n'), ['M/L', 'P/L', '\r\n', '\r\n', '\r\n'])
     self.assertEqual(find_optimal_sequence('Code 2D!'), [
         'C', 'L/L', 'o', 'd', 'e', 'D/L', ' ', '2', 'U/S', 'D', 'P/S', '!'])
     self.assertEqual(find_optimal_sequence('test 1!test 2!'), [
         'L/L', 't', 'e', 's', 't', 'D/L', ' ', '1', 'P/S', '!', 'U/L',
         'L/L', 't', 'e', 's', 't', 'D/L', ' ', '2', 'P/S', '!'])
     self.assertEqual(find_optimal_sequence('Abc-123X!Abc-123X!'), [
         'A', 'L/L', 'b', 'c', 'D/L', 'P/S', '-', '1', '2', '3', 'U/L', 'X', 'P/S', '!',
         'A', 'L/L', 'b', 'c', 'D/L', 'P/S', '-', '1', '2', '3', 'U/S', 'X', 'P/S', '!'])
     self.assertEqual(find_optimal_sequence('ABCabc1a2b3e'), [
         'A', 'B', 'C', 'L/L', 'a', 'b', 'c', 'B/S', 5, '1', 'a', '2', 'b', '3', 'e'])
     self.assertEqual(find_optimal_sequence('ABCabc1a2b3eBC'), [
         'A', 'B', 'C', 'L/L', 'a', 'b', 'c', 'B/S', 6, '1', 'a', '2', 'b', '3', 'e', 'M/L', 'U/L', 'B', 'C'])
     self.assertEqual(find_optimal_sequence('0a|5Tf.l'), [
         'D/L', '0', 'U/L', 'L/L', 'a', 'M/L', '|', 'U/L',
         'D/L', '5', 'U/L', 'L/L', 'U/S', 'T', 'f', 'P/S', '.', 'l'])
     self.assertEqual(find_optimal_sequence('*V1\x0c {Pa'), [
         'P/S', '*', 'V', 'B/S', 2, '1', '\x0c', ' ', 'P/S', '{', 'P', 'L/L', 'a'])
     self.assertEqual(find_optimal_sequence('~Fxlb"I4'), [
         'M/L', '~', 'U/L', 'D/L', 'U/S', 'F', 'U/L', 'L/L', 'x', 'l', 'b', 'D/L', 'P/S', '"', 'U/S', 'I', '4'])
     self.assertEqual(find_optimal_sequence('\\+=R?1'), [
         'M/L', '\\', 'P/L', '+', '=', 'U/L', 'R', 'D/L', 'P/S', '?', '1'])
Пример #4
0
    def test_find_optimal_sequence_ascii_strings(self):
        """ Test find_optimal_sequence function for ASCII strings """
        self.assertEqual(find_optimal_sequence(''), b())
        self.assertEqual(find_optimal_sequence('ABC'), b('A', 'B', 'C'))
        self.assertEqual(find_optimal_sequence('abc'),
                         b(Latch.LOWER, 'a', 'b', 'c'))
        self.assertEqual(
            find_optimal_sequence('Wikipedia, the free encyclopedia'),
            b('W', Latch.LOWER, 'i', 'k', 'i', 'p', 'e', 'd', 'i', 'a',
              Shift.PUNCT, ', ', 't', 'h', 'e', ' ', 'f', 'r', 'e', 'e', ' ',
              'e', 'n', 'c', 'y', 'c', 'l', 'o', 'p', 'e', 'd', 'i', 'a'))
        self.assertEqual(
            find_optimal_sequence('Code 2D!'),
            b('C', Latch.LOWER, 'o', 'd', 'e', Latch.DIGIT, ' ', '2',
              Shift.UPPER, 'D', Shift.PUNCT, '!'))
        self.assertEqual(
            find_optimal_sequence('!#$%&?'),
            b(Latch.MIXED, Latch.PUNCT, '!', '#', '$', '%', '&', '?'))

        self.assertIn(find_optimal_sequence('. : '),
                      (b(Shift.PUNCT, '. ', Shift.PUNCT,
                         ': '), b(Latch.MIXED, Latch.PUNCT, '. ', ': ')))
        self.assertEqual(find_optimal_sequence('\r\n\r\n\r\n'),
                         b(Latch.MIXED, Latch.PUNCT, '\r\n', '\r\n', '\r\n'))
        self.assertEqual(
            find_optimal_sequence('Code 2D!'),
            b('C', Latch.LOWER, 'o', 'd', 'e', Latch.DIGIT, ' ', '2',
              Shift.UPPER, 'D', Shift.PUNCT, '!'))
        self.assertEqual(
            find_optimal_sequence('test 1!test 2!'),
            b(Latch.LOWER, 't', 'e', 's', 't', Latch.DIGIT, ' ', '1',
              Shift.PUNCT, '!', Latch.UPPER, Latch.LOWER, 't', 'e', 's', 't',
              Latch.DIGIT, ' ', '2', Shift.PUNCT, '!'))
        self.assertEqual(
            find_optimal_sequence('Abc-123X!Abc-123X!'),
            b('A', Latch.LOWER, 'b', 'c', Latch.DIGIT, Shift.PUNCT, '-', '1',
              '2', '3', Latch.UPPER, 'X', Shift.PUNCT, '!', 'A', Latch.LOWER,
              'b', 'c', Latch.DIGIT, Shift.PUNCT, '-', '1', '2', '3',
              Shift.UPPER, 'X', Shift.PUNCT, '!'))
        self.assertEqual(
            find_optimal_sequence('ABCabc1a2b3e'),
            b('A', 'B', 'C', Latch.LOWER, 'a', 'b', 'c', Shift.BINARY, 5, '1',
              'a', '2', 'b', '3', 'e'))
        self.assertEqual(
            find_optimal_sequence('ABCabc1a2b3eBC'),
            b('A', 'B', 'C', Latch.LOWER, 'a', 'b', 'c', Shift.BINARY, 6, '1',
              'a', '2', 'b', '3', 'e', Latch.MIXED, Latch.UPPER, 'B', 'C'))
        self.assertEqual(
            find_optimal_sequence('0a|5Tf.l'),
            b(Shift.BINARY, 5, '0', 'a', '|', '5', 'T', Latch.LOWER, 'f',
              Shift.PUNCT, '.', 'l'))
        self.assertEqual(
            find_optimal_sequence('*V1\x0c {Pa'),
            b(Shift.PUNCT, '*', 'V', Shift.BINARY, 5, '1', '\x0c', ' ', '{',
              'P', Latch.LOWER, 'a'))
        self.assertEqual(
            find_optimal_sequence('~Fxlb"I4'),
            b(Shift.BINARY, 7, '~', 'F', 'x', 'l', 'b', '"', 'I', Latch.DIGIT,
              '4'))
        self.assertEqual(
            find_optimal_sequence('\\+=R?1'),
            b(Latch.MIXED, '\\', Latch.PUNCT, '+', '=', Latch.UPPER, 'R',
              Latch.DIGIT, Shift.PUNCT, '?', '1'))
        self.assertEqual(
            find_optimal_sequence('0123456789:;<=>'),
            b(Latch.DIGIT, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
              Latch.UPPER, Latch.MIXED, Latch.PUNCT, ':', ';', '<', '=', '>'))
 def test_find_optimal_sequence(self):
     """ Test find_optimal_sequence function """
     self.assertEqual(find_optimal_sequence(''), [])
     self.assertEqual(find_optimal_sequence('ABC'), ['A', 'B', 'C'])
     self.assertEqual(find_optimal_sequence('abc'), ['L/L', 'a', 'b', 'c'])
     self.assertEqual(
         find_optimal_sequence('Wikipedia, the free encyclopedia'), [
             'W', 'L/L', 'i', 'k', 'i', 'p', 'e', 'd', 'i', 'a', 'P/S',
             ', ', 't', 'h', 'e', ' ', 'f', 'r', 'e', 'e', ' ', 'e', 'n',
             'c', 'y', 'c', 'l', 'o', 'p', 'e', 'd', 'i', 'a'
         ])
     self.assertEqual(find_optimal_sequence('Code 2D!'), [
         'C', 'L/L', 'o', 'd', 'e', 'D/L', ' ', '2', 'U/S', 'D', 'P/S', '!'
     ])
     self.assertEqual(find_optimal_sequence('a\xff'),
                      ['B/S', 2, 'a', '\xff'])
     self.assertEqual(find_optimal_sequence('a' + '\xff' * 30),
                      ['B/S', 31, 'a'] + ['\xff'] * 30)
     self.assertEqual(find_optimal_sequence('a' + '\xff' * 31),
                      ['B/S', 0, 32, 'a'] + ['\xff'] * 31)
     self.assertEqual(find_optimal_sequence('!#$%&?'),
                      ['M/L', 'P/L', '!', '#', '$', '%', '&', '?'])
     self.assertEqual(find_optimal_sequence('!#$%&?\xff'), [
         'M/L', 'P/L', '!', '#', '$', '%', '&', '?', 'U/L', 'B/S', 1, '\xff'
     ])
     self.assertEqual(find_optimal_sequence('!#$%&\xff'),
                      ['B/S', 6, '!', '#', '$', '%', '&', '\xff'])
     self.assertEqual(find_optimal_sequence('@\xff'),
                      ['B/S', 2, '@', '\xff'])
     self.assertEqual(find_optimal_sequence('. @\xff'),
                      ['P/S', '. ', 'B/S', 2, '@', '\xff'])
     self.assertIn(find_optimal_sequence('. : '),
                   [['P/S', '. ', 'P/S', ': '], ['M/L', 'P/L', '. ', ': ']])
     self.assertEqual(find_optimal_sequence('\r\n\r\n\r\n'),
                      ['M/L', 'P/L', '\r\n', '\r\n', '\r\n'])
     self.assertEqual(find_optimal_sequence('Code 2D!'), [
         'C', 'L/L', 'o', 'd', 'e', 'D/L', ' ', '2', 'U/S', 'D', 'P/S', '!'
     ])
     self.assertEqual(find_optimal_sequence('test 1!test 2!'), [
         'L/L', 't', 'e', 's', 't', 'D/L', ' ', '1', 'P/S', '!', 'U/L',
         'L/L', 't', 'e', 's', 't', 'D/L', ' ', '2', 'P/S', '!'
     ])
     self.assertEqual(find_optimal_sequence('Abc-123X!Abc-123X!'), [
         'A', 'L/L', 'b', 'c', 'D/L', 'P/S', '-', '1', '2', '3', 'U/L', 'X',
         'P/S', '!', 'A', 'L/L', 'b', 'c', 'D/L', 'P/S', '-', '1', '2', '3',
         'U/S', 'X', 'P/S', '!'
     ])
     self.assertEqual(find_optimal_sequence('ABCabc1a2b3e'), [
         'A', 'B', 'C', 'L/L', 'a', 'b', 'c', 'B/S', 5, '1', 'a', '2', 'b',
         '3', 'e'
     ])
     self.assertEqual(find_optimal_sequence('ABCabc1a2b3eBC'), [
         'A', 'B', 'C', 'L/L', 'a', 'b', 'c', 'B/S', 6, '1', 'a', '2', 'b',
         '3', 'e', 'M/L', 'U/L', 'B', 'C'
     ])
     self.assertEqual(find_optimal_sequence('0a|5Tf.l'), [
         'D/L', '0', 'U/L', 'L/L', 'a', 'M/L', '|', 'U/L', 'D/L', '5',
         'U/L', 'L/L', 'U/S', 'T', 'f', 'P/S', '.', 'l'
     ])
     self.assertEqual(find_optimal_sequence('*V1\x0c {Pa'), [
         'P/S', '*', 'V', 'B/S', 2, '1', '\x0c', ' ', 'P/S', '{', 'P',
         'L/L', 'a'
     ])
     self.assertEqual(find_optimal_sequence('~Fxlb"I4'), [
         'M/L', '~', 'U/L', 'D/L', 'U/S', 'F', 'U/L', 'L/L', 'x', 'l', 'b',
         'D/L', 'P/S', '"', 'U/S', 'I', '4'
     ])
     self.assertEqual(
         find_optimal_sequence('\\+=R?1'),
         ['M/L', '\\', 'P/L', '+', '=', 'U/L', 'R', 'D/L', 'P/S', '?', '1'])
 def test_find_optimal_sequence(self):
     """ Test find_optimal_sequence function """
     self.assertEqual(find_optimal_sequence(''), [])
     self.assertEqual(find_optimal_sequence('ABC'), ['A', 'B', 'C'])
     self.assertEqual(find_optimal_sequence('abc'), ['L/L', 'a', 'b', 'c'])
     self.assertEqual(
         find_optimal_sequence('Wikipedia, the free encyclopedia'), [
             'W', 'L/L', 'i', 'k', 'i', 'p', 'e', 'd', 'i', 'a', 'P/S',
             ', ', 't', 'h', 'e', ' ', 'f', 'r', 'e', 'e', ' ', 'e', 'n',
             'c', 'y', 'c', 'l', 'o', 'p', 'e', 'd', 'i', 'a'
         ])
     self.assertEqual(find_optimal_sequence('Code 2D!'), [
         'C', 'L/L', 'o', 'd', 'e', 'D/L', ' ', '2', 'U/S', 'D', 'P/S', '!'
     ])
     self.assertEqual(find_optimal_sequence('a\xff'),
                      ['B/S', 2, 'a', '\xff'])
     self.assertEqual(find_optimal_sequence('a' + '\xff' * 30),
                      ['B/S', 31, 'a'] + ['\xff'] * 30)
     self.assertEqual(find_optimal_sequence('a' + '\xff' * 31),
                      ['B/S', 0, 32, 'a'] + ['\xff'] * 31)
     self.assertEqual(find_optimal_sequence('!#$%&?'),
                      ['M/L', 'P/L', '!', '#', '$', '%', '&', '?'])
     self.assertEqual(find_optimal_sequence('!#$%&?\xff'), [
         'M/L', 'P/L', '!', '#', '$', '%', '&', '?', 'U/L', 'B/S', 1, '\xff'
     ])
     self.assertEqual(find_optimal_sequence('!#$%&\xff'),
                      ['B/S', 6, '!', '#', '$', '%', '&', '\xff'])
     self.assertEqual(find_optimal_sequence('@\xff'),
                      ['B/S', 2, '@', '\xff'])
     self.assertEqual(find_optimal_sequence('. @\xff'),
                      ['P/S', '. ', 'B/S', 2, '@', '\xff'])
     self.assertEqual(find_optimal_sequence('. : '),
                      ['P/S', '. ', 'P/S', ': '])
     self.assertEqual(find_optimal_sequence('\r\n\r\n\r\n'),
                      ['M/L', 'P/L', '\r\n', '\r\n', '\r\n'])
     self.assertEqual(find_optimal_sequence('Code 2D!'), [
         'C', 'L/L', 'o', 'd', 'e', 'D/L', ' ', '2', 'U/S', 'D', 'P/S', '!'
     ])
     self.assertEqual(find_optimal_sequence('test 1!test 2!'), [
         'L/L', 't', 'e', 's', 't', 'D/L', ' ', '1', 'P/S', '!', 'U/L',
         'L/L', 't', 'e', 's', 't', 'D/L', ' ', '2', 'P/S', '!'
     ])
     self.assertEqual(find_optimal_sequence('Abc-123X!Abc-123X!'), [
         'A', 'L/L', 'b', 'c', 'D/L', 'P/S', '-', '1', '2', '3', 'U/L', 'X',
         'P/S', '!', 'A', 'L/L', 'b', 'c', 'D/L', 'P/S', '-', '1', '2', '3',
         'U/S', 'X', 'P/S', '!'
     ])