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