4e00021525fccb0b5694a788be9d87da22108520
[kai/samba.git] / source3 / stf / strings.py
1 #! /usr/bin/python     
2
3 # Comfychair test cases for Samba string functions.
4
5 # Copyright (C) 2003 by Martin Pool <mbp@samba.org>
6
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 3 of the
10 # License, or (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, see <http://www.gnu.org/licenses/>.
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 # Note that this is NOT the case in C code until the loadparm table is
26 # intialized -- the default seems to be ASCII, which rather lets Samba
27 # off the hook. :-) The best way seems to be to put this in the test
28 # harnesses:
29 #
30 #       lp_load("/dev/null", True, False, False);
31 #
32 # -- mbp
33
34 import sys, re, comfychair
35 from unicodenames import *
36
37 def signum(a):
38     if a < 0:
39         return -1
40     elif a > 0:
41         return +1
42     else:
43         return 0
44
45
46 class PushUCS2_Tests(comfychair.TestCase):
47     """Conversion to/from UCS2"""
48     def runtest(self):
49         OE = LATIN_CAPITAL_LETTER_O_WITH_DIARESIS
50         oe = LATIN_CAPITAL_LETTER_O_WITH_DIARESIS
51         cases = ['hello',
52                  'hello world',
53                  'g' + OE + OE + 'gomobile', 
54                  'g' + OE + oe + 'gomobile', 
55                  u'foo\u0100',
56                  KATAKANA_LETTER_A * 20,
57                  ]
58         for u8str in cases:
59             out, err = self.runcmd("t_push_ucs2 \"%s\"" % u8str.encode('utf-8'))
60             self.assert_equal(out, "0\n")
61     
62
63 class StrCaseCmp(comfychair.TestCase):
64     """String comparisons in simple ASCII""" 
65     def run_strcmp(self, a, b, expect):
66         out, err = self.runcmd('t_strcmp \"%s\" \"%s\"' % (a.encode('utf-8'), b.encode('utf-8')))
67         if signum(int(out)) != expect:
68             self.fail("comparison failed:\n"
69                       "  a=%s\n"
70                       "  b=%s\n"
71                       "  expected=%s\n"
72                       "  result=%s\n" % (`a`, `b`, `expect`, `out`))
73
74     def runtest(self):
75         # A, B, strcasecmp(A, B)
76         cases = [('hello', 'hello', 0),
77                  ('hello', 'goodbye', +1),
78                  ('goodbye', 'hello', -1),
79                  ('hell', 'hello', -1),
80                  ('', '', 0),
81                  ('a', '', +1),
82                  ('', 'a', -1),
83                  ('a', 'A', 0),
84                  ('aa', 'aA', 0),
85                  ('Aa', 'aa', 0),
86                  ('longstring ' * 100, 'longstring ' * 100, 0),
87                  ('longstring ' * 100, 'longstring ' * 100 + 'a', -1),
88                  ('longstring ' * 100 + 'a', 'longstring ' * 100, +1),
89                  (KATAKANA_LETTER_A, KATAKANA_LETTER_A, 0),
90                  (KATAKANA_LETTER_A, 'a', 1),
91                  ]
92         for a, b, expect in cases:
93             self.run_strcmp(a, b, expect)
94         
95 class strstr_m(comfychair.TestCase):
96     """String comparisons in simple ASCII""" 
97     def run_strstr(self, a, b, expect):
98         out, err = self.runcmd('t_strstr \"%s\" \"%s\"' % (a.encode('utf-8'), b.encode('utf-8')))
99         if (out != (expect + '\n').encode('utf-8')):
100             self.fail("comparison failed:\n"
101                       "  a=%s\n"
102                       "  b=%s\n"
103                       "  expected=%s\n"
104                       "  result=%s\n" % (`a`, `b`, `expect+'\n'`, `out`))
105
106     def runtest(self):
107         # A, B, strstr_m(A, B)
108         cases = [('hello', 'hello', 'hello'),
109                  ('hello', 'goodbye', '(null)'),
110                  ('goodbye', 'hello', '(null)'),
111                  ('hell', 'hello', '(null)'),
112                  ('hello', 'hell', 'hello'),
113                  ('', '', ''),
114                  ('a', '', 'a'),
115                  ('', 'a', '(null)'),
116                  ('a', 'A', '(null)'),
117                  ('aa', 'aA', '(null)'),
118                  ('Aa', 'aa', '(null)'),
119                  ('%v foo', '%v', '%v foo'),
120                  ('foo %v foo', '%v', '%v foo'),
121                  ('foo %v', '%v', '%v'),
122                  ('longstring ' * 100, 'longstring ' * 99, 'longstring ' * 100),
123                  ('longstring ' * 99, 'longstring ' * 100, '(null)'),
124                  ('longstring a' * 99, 'longstring ' * 100 + 'a', '(null)'),
125                  ('longstring ' * 100 + 'a', 'longstring ' * 100, 'longstring ' * 100 + 'a'),
126                  (KATAKANA_LETTER_A, KATAKANA_LETTER_A + 'bcd', '(null)'),
127                  (KATAKANA_LETTER_A + 'bcde', KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcde'),
128                  ('d'+KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcd'),
129                  ('d'+KATAKANA_LETTER_A + 'bd', KATAKANA_LETTER_A + 'bcd', '(null)'),
130                  
131                  ('e'+KATAKANA_LETTER_A + 'bcdf', KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcdf'),
132                  (KATAKANA_LETTER_A, KATAKANA_LETTER_A + 'bcd', '(null)'),
133                  (KATAKANA_LETTER_A*3, 'a', '(null)'),
134                  ]
135         for a, b, expect in cases:
136             self.run_strstr(a, b, expect)
137         
138 # Define the tests exported by this module
139 tests = [StrCaseCmp,
140          strstr_m,
141          PushUCS2_Tests]
142
143 # Handle execution of this file as a main program
144 if __name__ == '__main__':
145     comfychair.main(tests)
146
147 # Local variables:
148 # coding: utf-8
149 # End: