2011-05-18 06:06:25 +04:00
# subunit test cases for Samba string functions.
2003-03-19 06:42:27 +03:00
# Copyright (C) 2003 by Martin Pool <mbp@samba.org>
2011-05-18 06:06:25 +04:00
# Copyright (C) 2011 Andrew Bartlett
#
2011-09-13 03:10:37 +04:00
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
2011-05-18 06:06:25 +04:00
#
2011-09-13 03:10:37 +04:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2011-05-18 06:06:25 +04:00
#
2003-03-19 06:42:27 +03:00
# You should have received a copy of the GNU General Public License
2011-09-13 03:10:37 +04:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2003-03-19 06:42:27 +03:00
2003-04-14 04:21:52 +04:00
# XXX: All this code assumes that the Unix character set is UTF-8,
# which is the most common setting. I guess it would be better to
# force it to that value while running the tests. I'm not sure of the
# best way to do that yet.
#
# -- mbp
2018-10-24 05:56:20 +03:00
import unicodedata
2011-05-18 06:06:25 +04:00
import samba . tests
from samba import strcasecmp_m , strstr_m
2018-07-30 09:20:39 +03:00
2018-10-24 05:56:20 +03:00
KATAKANA_LETTER_A = unicodedata . lookup ( " KATAKANA LETTER A " )
2003-03-19 06:42:27 +03:00
def signum ( a ) :
if a < 0 :
return - 1
elif a > 0 :
return + 1
else :
return 0
2003-04-14 04:21:52 +04:00
2011-05-18 06:06:25 +04:00
class strcasecmp_m_Tests ( samba . tests . TestCase ) :
""" String comparisons in simple ASCII and unicode """
def test_strcasecmp_m ( self ) :
2003-03-19 11:32:42 +03:00
# A, B, strcasecmp(A, B)
2003-03-19 06:42:27 +03:00
cases = [ ( ' hello ' , ' hello ' , 0 ) ,
( ' hello ' , ' goodbye ' , + 1 ) ,
( ' goodbye ' , ' hello ' , - 1 ) ,
2003-03-19 11:32:42 +03:00
( ' hell ' , ' hello ' , - 1 ) ,
( ' ' , ' ' , 0 ) ,
( ' a ' , ' ' , + 1 ) ,
( ' ' , ' a ' , - 1 ) ,
( ' a ' , ' A ' , 0 ) ,
( ' aa ' , ' aA ' , 0 ) ,
( ' Aa ' , ' aa ' , 0 ) ,
( ' longstring ' * 100 , ' longstring ' * 100 , 0 ) ,
( ' longstring ' * 100 , ' longstring ' * 100 + ' a ' , - 1 ) ,
( ' longstring ' * 100 + ' a ' , ' longstring ' * 100 , + 1 ) ,
2003-04-14 04:21:52 +04:00
( KATAKANA_LETTER_A , KATAKANA_LETTER_A , 0 ) ,
( KATAKANA_LETTER_A , ' a ' , 1 ) ,
2003-03-19 11:32:42 +03:00
]
2003-03-19 06:42:27 +03:00
for a , b , expect in cases :
2020-02-07 01:02:38 +03:00
self . assertEqual ( signum ( strcasecmp_m ( a , b ) ) , expect )
2004-03-09 14:15:44 +03:00
2018-07-30 09:20:39 +03:00
2011-05-18 06:06:25 +04:00
class strstr_m_Tests ( samba . tests . TestCase ) :
""" strstr_m tests in simple ASCII and unicode strings """
2012-02-19 02:59:48 +04:00
2011-05-18 06:06:25 +04:00
def test_strstr_m ( self ) :
2004-03-09 14:15:44 +03:00
# A, B, strstr_m(A, B)
cases = [ ( ' hello ' , ' hello ' , ' hello ' ) ,
2011-05-18 06:06:25 +04:00
( ' hello ' , ' goodbye ' , None ) ,
( ' goodbye ' , ' hello ' , None ) ,
( ' hell ' , ' hello ' , None ) ,
2004-03-09 14:15:44 +03:00
( ' hello ' , ' hell ' , ' hello ' ) ,
( ' ' , ' ' , ' ' ) ,
( ' a ' , ' ' , ' a ' ) ,
2011-05-18 06:06:25 +04:00
( ' ' , ' a ' , None ) ,
( ' a ' , ' A ' , None ) ,
( ' aa ' , ' aA ' , None ) ,
( ' Aa ' , ' aa ' , None ) ,
2004-03-09 14:15:44 +03:00
( ' % v foo ' , ' % v ' , ' % v foo ' ) ,
( ' foo % v foo ' , ' % v ' , ' % v foo ' ) ,
( ' foo % v ' , ' % v ' , ' % v ' ) ,
( ' longstring ' * 100 , ' longstring ' * 99 , ' longstring ' * 100 ) ,
2011-05-18 06:06:25 +04:00
( ' longstring ' * 99 , ' longstring ' * 100 , None ) ,
( ' longstring a ' * 99 , ' longstring ' * 100 + ' a ' , None ) ,
2004-03-09 14:15:44 +03:00
( ' longstring ' * 100 + ' a ' , ' longstring ' * 100 , ' longstring ' * 100 + ' a ' ) ,
2011-05-18 06:06:25 +04:00
( KATAKANA_LETTER_A , KATAKANA_LETTER_A + ' bcd ' , None ) ,
2004-03-09 14:15:44 +03:00
( KATAKANA_LETTER_A + ' bcde ' , KATAKANA_LETTER_A + ' bcd ' , KATAKANA_LETTER_A + ' bcde ' ) ,
2018-07-30 09:18:25 +03:00
( ' d ' + KATAKANA_LETTER_A + ' bcd ' , KATAKANA_LETTER_A + ' bcd ' , KATAKANA_LETTER_A + ' bcd ' ) ,
( ' d ' + KATAKANA_LETTER_A + ' bd ' , KATAKANA_LETTER_A + ' bcd ' , None ) ,
2011-05-18 06:06:25 +04:00
2018-07-30 09:18:25 +03:00
( ' e ' + KATAKANA_LETTER_A + ' bcdf ' , KATAKANA_LETTER_A + ' bcd ' , KATAKANA_LETTER_A + ' bcdf ' ) ,
2011-05-18 06:06:25 +04:00
( KATAKANA_LETTER_A , KATAKANA_LETTER_A + ' bcd ' , None ) ,
2018-07-30 09:18:25 +03:00
( KATAKANA_LETTER_A * 3 , ' a ' , None ) ,
2004-03-09 14:15:44 +03:00
]
for a , b , expect in cases :
2020-02-07 01:02:38 +03:00
self . assertEqual ( strstr_m ( a , b ) , expect )