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 2 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, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20# USA
21
22# XXX: All this code assumes that the Unix character set is UTF-8,
23# which is the most common setting.  I guess it would be better to
24# force it to that value while running the tests.  I'm not sure of the
25# best way to do that yet.
26#
27# Note that this is NOT the case in C code until the loadparm table is
28# intialized -- the default seems to be ASCII, which rather lets Samba
29# off the hook. :-) The best way seems to be to put this in the test
30# harnesses:
31#
32#       lp_load("/dev/null", True, False, False);
33#
34# -- mbp
35
36import sys, re, comfychair
37from unicodenames import *
38
39def signum(a):
40    if a < 0:
41        return -1
42    elif a > 0:
43        return +1
44    else:
45        return 0
46
47
48class PushUCS2_Tests(comfychair.TestCase):
49    """Conversion to/from UCS2"""
50    def runtest(self):
51        OE = LATIN_CAPITAL_LETTER_O_WITH_DIARESIS
52        oe = LATIN_CAPITAL_LETTER_O_WITH_DIARESIS
53        cases = ['hello',
54                 'hello world',
55                 'g' + OE + OE + 'gomobile',
56                 'g' + OE + oe + 'gomobile',
57                 u'foo\u0100',
58                 KATAKANA_LETTER_A * 20,
59                 ]
60        for u8str in cases:
61            out, err = self.runcmd("t_push_ucs2 \"%s\"" % u8str.encode('utf-8'))
62            self.assert_equal(out, "0\n")
63
64
65class StrCaseCmp(comfychair.TestCase):
66    """String comparisons in simple ASCII"""
67    def run_strcmp(self, a, b, expect):
68        out, err = self.runcmd('t_strcmp \"%s\" \"%s\"' % (a.encode('utf-8'), b.encode('utf-8')))
69        if signum(int(out)) != expect:
70            self.fail("comparison failed:\n"
71                      "  a=%s\n"
72                      "  b=%s\n"
73                      "  expected=%s\n"
74                      "  result=%s\n" % (`a`, `b`, `expect`, `out`))
75
76    def runtest(self):
77        # A, B, strcasecmp(A, B)
78        cases = [('hello', 'hello', 0),
79                 ('hello', 'goodbye', +1),
80                 ('goodbye', 'hello', -1),
81                 ('hell', 'hello', -1),
82                 ('', '', 0),
83                 ('a', '', +1),
84                 ('', 'a', -1),
85                 ('a', 'A', 0),
86                 ('aa', 'aA', 0),
87                 ('Aa', 'aa', 0),
88                 ('longstring ' * 100, 'longstring ' * 100, 0),
89                 ('longstring ' * 100, 'longstring ' * 100 + 'a', -1),
90                 ('longstring ' * 100 + 'a', 'longstring ' * 100, +1),
91                 (KATAKANA_LETTER_A, KATAKANA_LETTER_A, 0),
92                 (KATAKANA_LETTER_A, 'a', 1),
93                 ]
94        for a, b, expect in cases:
95            self.run_strcmp(a, b, expect)
96
97class strstr_m(comfychair.TestCase):
98    """String comparisons in simple ASCII"""
99    def run_strstr(self, a, b, expect):
100        out, err = self.runcmd('t_strstr \"%s\" \"%s\"' % (a.encode('utf-8'), b.encode('utf-8')))
101        if (out != (expect + '\n').encode('utf-8')):
102            self.fail("comparison failed:\n"
103                      "  a=%s\n"
104                      "  b=%s\n"
105                      "  expected=%s\n"
106                      "  result=%s\n" % (`a`, `b`, `expect+'\n'`, `out`))
107
108    def runtest(self):
109        # A, B, strstr_m(A, B)
110        cases = [('hello', 'hello', 'hello'),
111                 ('hello', 'goodbye', '(null)'),
112                 ('goodbye', 'hello', '(null)'),
113                 ('hell', 'hello', '(null)'),
114                 ('hello', 'hell', 'hello'),
115                 ('', '', ''),
116                 ('a', '', 'a'),
117                 ('', 'a', '(null)'),
118                 ('a', 'A', '(null)'),
119                 ('aa', 'aA', '(null)'),
120                 ('Aa', 'aa', '(null)'),
121                 ('%v foo', '%v', '%v foo'),
122                 ('foo %v foo', '%v', '%v foo'),
123                 ('foo %v', '%v', '%v'),
124                 ('longstring ' * 100, 'longstring ' * 99, 'longstring ' * 100),
125                 ('longstring ' * 99, 'longstring ' * 100, '(null)'),
126                 ('longstring a' * 99, 'longstring ' * 100 + 'a', '(null)'),
127                 ('longstring ' * 100 + 'a', 'longstring ' * 100, 'longstring ' * 100 + 'a'),
128                 (KATAKANA_LETTER_A, KATAKANA_LETTER_A + 'bcd', '(null)'),
129                 (KATAKANA_LETTER_A + 'bcde', KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcde'),
130                 ('d'+KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcd'),
131                 ('d'+KATAKANA_LETTER_A + 'bd', KATAKANA_LETTER_A + 'bcd', '(null)'),
132
133                 ('e'+KATAKANA_LETTER_A + 'bcdf', KATAKANA_LETTER_A + 'bcd', KATAKANA_LETTER_A + 'bcdf'),
134                 (KATAKANA_LETTER_A, KATAKANA_LETTER_A + 'bcd', '(null)'),
135                 (KATAKANA_LETTER_A*3, 'a', '(null)'),
136                 ]
137        for a, b, expect in cases:
138            self.run_strstr(a, b, expect)
139
140# Define the tests exported by this module
141tests = [StrCaseCmp,
142         strstr_m,
143         PushUCS2_Tests]
144
145# Handle execution of this file as a main program
146if __name__ == '__main__':
147    comfychair.main(tests)
148
149# Local variables:
150# coding: utf-8
151# End:
152