PEP8: fix E225: missing whitespace around operator
[nivanova/samba-autobuild/.git] / python / samba / tests / strings.py
1 # subunit test cases for Samba string functions.
2
3 # Copyright (C) 2003 by Martin Pool <mbp@samba.org>
4 # Copyright (C) 2011 Andrew Bartlett
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 # XXX: All this code assumes that the Unix character set is UTF-8,
21 # which is the most common setting.  I guess it would be better to
22 # force it to that value while running the tests.  I'm not sure of the
23 # best way to do that yet.
24 #
25 # -- mbp
26
27 from unicodenames import *
28
29 import samba.tests
30 from samba import strcasecmp_m, strstr_m
31
32 def signum(a):
33     if a < 0:
34         return -1
35     elif a > 0:
36         return +1
37     else:
38         return 0
39
40
41 class strcasecmp_m_Tests(samba.tests.TestCase):
42     """String comparisons in simple ASCII and unicode"""
43     def test_strcasecmp_m(self):
44         # A, B, strcasecmp(A, B)
45         cases = [('hello', 'hello', 0),
46                  ('hello', 'goodbye', +1),
47                  ('goodbye', 'hello', -1),
48                  ('hell', 'hello', -1),
49                  ('', '', 0),
50                  ('a', '', +1),
51                  ('', 'a', -1),
52                  ('a', 'A', 0),
53                  ('aa', 'aA', 0),
54                  ('Aa', 'aa', 0),
55                  ('longstring ' * 100, 'longstring ' * 100, 0),
56                  ('longstring ' * 100, 'longstring ' * 100 + 'a', -1),
57                  ('longstring ' * 100 + 'a', 'longstring ' * 100, +1),
58                  (KATAKANA_LETTER_A, KATAKANA_LETTER_A, 0),
59                  (KATAKANA_LETTER_A, 'a', 1),
60                  ]
61         for a, b, expect in cases:
62             self.assertEquals(signum(strcasecmp_m(a.encode('utf-8'),
63                                                   b.encode('utf-8'))),
64                               expect)
65
66 class strstr_m_Tests(samba.tests.TestCase):
67     """strstr_m tests in simple ASCII and unicode strings"""
68
69     def test_strstr_m(self):
70         # A, B, strstr_m(A, B)
71         cases = [('hello', 'hello', 'hello'),
72                  ('hello', 'goodbye', None),
73                  ('goodbye', 'hello', None),
74                  ('hell', 'hello', None),
75                  ('hello', 'hell', 'hello'),
76                  ('', '', ''),
77                  ('a', '', 'a'),
78                  ('', 'a', None),
79                  ('a', 'A', None),
80                  ('aa', 'aA', None),
81                  ('Aa', 'aa', None),
82                  ('%v foo', '%v', '%v foo'),
83                  ('foo %v foo', '%v', '%v foo'),
84                  ('foo %v', '%v', '%v'),
85                  ('longstring ' * 100, 'longstring ' * 99, 'longstring ' * 100),
86                  ('longstring ' * 99, 'longstring ' * 100, None),
87                  ('longstring a' * 99, 'longstring ' * 100 + 'a', None),
88                  ('longstring ' * 100 + 'a', 'longstring ' * 100, 'longstring ' * 100 + 'a'),
89                  (KATAKANA_LETTER_A, KATAKANA_LETTER_A + 'bcd', None),
90                  (KATAKANA_LETTER_A + 'bcde', KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcde'),
91                  ('d'+KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcd'),
92                  ('d'+KATAKANA_LETTER_A + 'bd', KATAKANA_LETTER_A + 'bcd', None),
93
94                  ('e'+KATAKANA_LETTER_A + 'bcdf', KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcdf'),
95                  (KATAKANA_LETTER_A, KATAKANA_LETTER_A + 'bcd', None),
96                  (KATAKANA_LETTER_A*3, 'a', None),
97                  ]
98         for a, b, expect in cases:
99             if expect is not None:
100                 expect = expect.encode('utf-8')
101             self.assertEquals(strstr_m(a.encode('utf-8'),
102                                        b.encode('utf-8')),
103                               expect)