test_StringUtil.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 
00004 #
00005 # \file test_StringUtil.py
00006 # \brief test for String operation utility
00007 # \date $Date: $
00008 # \author Shinji Kurihara
00009 #
00010 # Copyright (C) 2003-2005
00011 #     Task-intelligence Research Group,
00012 #     Intelligent Systems Research Institute,
00013 #     National Institute of
00014 #         Advanced Industrial Science and Technology (AIST), Japan
00015 #     All rights reserved.
00016 #
00017 
00018 import sys
00019 sys.path.insert(1,"../")
00020 
00021 import unittest
00022 
00023 from StringUtil import *
00024 import OpenRTM_aist
00025 
00026 class TestStringUtil(unittest.TestCase):
00027 
00028   def setUp(self):
00029     pass
00030 
00031   def tearDown(self):
00032     OpenRTM_aist.Manager.instance().shutdownManager()
00033     pass
00034   
00035   def test_isEscaped(self):
00036     self.assertEqual(isEscaped("test\\\\test",5), True, "Result failed.")
00037 
00038     
00039   def test_escape(self):
00040     self.assertEqual(escape("test\ttest"), "test\\ttest", "Result failed.")
00041     self.assertEqual(escape("test\ntest"), "test\\ntest", "Result failed.")
00042     self.assertEqual(escape("test\ftest"), "test\\ftest", "Result failed.")
00043     self.assertEqual(escape("test\rtest"), "test\\rtest", "Result failed.")
00044 
00045 
00046   def test_unescape(self):
00047     self.assertEqual(unescape("test\\ttest"), "test\ttest", "Result failed.")
00048     self.assertEqual(unescape("test\\ntest"), "test\ntest", "Result failed.")
00049     self.assertEqual(unescape("test\\ftest"), "test\ftest", "Result failed.")
00050     self.assertEqual(unescape("test\\rtest"), "test\rtest", "Result failed.")
00051 
00052 
00053   def test_eraseBlank(self):
00054     _str=[" test"]
00055     eraseBlank(_str)
00056     self.assertEqual(_str[0], "test", "Result failed.")
00057 
00058     _str=["   test"]
00059     eraseBlank(_str)
00060     self.assertEqual(_str[0], "test", "Result failed.")
00061 
00062     _str=["\t\ttest"]
00063     eraseBlank(_str)
00064     self.assertEqual(_str[0], "test", "Result failed.")
00065 
00066     _str=[""]
00067     eraseBlank(_str)
00068     self.assertEqual(_str[0], "", "Result failed.")
00069 
00070     _str=["\t\n test"]
00071     eraseBlank(_str)
00072     self.assertEqual(_str[0], "\ntest")
00073 
00074     _str=["\t\\t test"]
00075     eraseBlank(_str)
00076     self.assertEqual(_str[0], "\\ttest", "Result failed.")
00077     
00078   def test_eraseHeadBlank(self):
00079     _str=[" test"]
00080     eraseHeadBlank(_str)
00081     self.assertEqual(_str[0], "test", "Result failed.")
00082 
00083     _str=["   test"]
00084     eraseHeadBlank(_str)
00085     self.assertEqual(_str[0], "test", "Result failed.")
00086 
00087     _str=["\t\ttest"]
00088     eraseHeadBlank(_str)
00089     self.assertEqual(_str[0], "test", "Result failed.")
00090 
00091     _str=[""]
00092     eraseHeadBlank(_str)
00093     self.assertEqual(_str[0], "", "Result failed.")
00094 
00095     _str=["\t\n test"]
00096     eraseHeadBlank(_str)
00097     self.assertEqual(_str[0], "\n test", "Result failed.")
00098 
00099     _str=["\t\\t test"]
00100     eraseHeadBlank(_str)
00101     self.assertEqual(_str[0], "\\t test", "Result failed.")
00102 
00103 
00104     # failed case
00105     # _str=["\\t\\ttest"]
00106     # eraseHeadBlank(_str)
00107     # self.assertEqual(_str[0], "test", "Result failed.")
00108 
00109 
00110   def test_eraseTailBlank(self):
00111     _str=["test "]
00112     eraseTailBlank(_str)
00113     self.assertEqual(_str[0], "test", "Result failed.")
00114 
00115     _str=["test   "]
00116     eraseTailBlank(_str)
00117     self.assertEqual(_str[0], "test", "Result failed.")
00118 
00119     _str=["test\t\t"]
00120     eraseTailBlank(_str)
00121     self.assertEqual(_str[0], "test", "Result failed.")
00122 
00123     _str=["test\t\n\t"]
00124     eraseTailBlank(_str)
00125     self.assertEqual(_str[0], "test\t\n", "Result failed.")
00126 
00127     _str=["test\\t"]
00128     eraseTailBlank(_str)
00129     self.assertEqual(_str[0], "test\\t", "Result failed.")
00130 
00131     # failed case
00132     # _str=["test\\t\\t"]
00133     # eraseTailBlank(_str)
00134     # self.assertEqual(_str[0], "test", "Result failed.")
00135 
00136   def test_normalize(self):
00137     _str = [" NORMALIZE "]
00138     normalize(_str)
00139     self.assertEqual(_str[0],"normalize")
00140 
00141     _str = [" \t  \tNORmALIZE "]
00142     normalize(_str)
00143     self.assertEqual(_str[0],"normalize")
00144 
00145     _str = [" nORMALIZE\t \t\t\t"]
00146     normalize(_str)
00147     self.assertEqual(_str[0],"normalize")
00148 
00149 
00150   def test_replaceString(self):
00151     _str= ["replace"]
00152     replaceString(_str,"l", "r")
00153     self.assertEqual(_str[0], "reprace", "Result failed.")
00154 
00155     _str= ["replace"]
00156     replaceString(_str,"re", "")
00157     self.assertEqual(_str[0], "place", "Result failed.")
00158 
00159     _str= ["replace"]
00160     replaceString(_str,"e", "a")
00161     self.assertEqual(_str[0], "raplaca", "Result failed.")
00162 
00163 
00164   def test_split(self):
00165     _str = split("test0,test1,test2", ",")
00166     self.assertEqual(_str[0], "test0", "Result failed.")
00167     self.assertEqual(_str[1], "test1", "Result failed.")
00168     self.assertEqual(_str[2], "test2", "Result failed.")
00169 
00170     _str = split("test0.test1.test2", ".")
00171     self.assertEqual(_str[0], "test0", "Result failed.")
00172     self.assertEqual(_str[1], "test1", "Result failed.")
00173     self.assertEqual(_str[2], "test2", "Result failed.")
00174 
00175     _str = split("test0/test1/test2", "/")
00176     self.assertEqual(_str[0], "test0", "Result failed.")
00177     self.assertEqual(_str[1], "test1", "Result failed.")
00178     self.assertEqual(_str[2], "test2", "Result failed.")
00179 
00180     _str = split("test0 test1 test2", " ")
00181     self.assertEqual(_str[0], "test0", "Result failed.")
00182     self.assertEqual(_str[1], "test1", "Result failed.")
00183     self.assertEqual(_str[2], "test2", "Result failed.")
00184 
00185 
00186   def test_toBool(self):
00187     ret = toBool("yes", "yes", "no", True)
00188     self.assertEqual(ret, True, "Result failed.")
00189 
00190     ret = toBool("no", "yes", "no", True)
00191     self.assertEqual(ret, False, "Result failed.")
00192 
00193     ret = toBool("Yes", "YES", "NO", True)
00194     self.assertEqual(ret, True, "Result failed.")
00195 
00196     ret = toBool("No", "YES", "NO", True)
00197     self.assertEqual(ret, False, "Result failed.")
00198 
00199   def test_includes(self):
00200     self.assertEqual(includes(["abc","abcde","ABC"],"abc"),True)
00201     self.assertEqual(includes(["abc","abcde","ABC"],"a"),False)
00202     self.assertEqual(includes(["abc","abcde","ABC"],"ABC"),True)
00203     self.assertEqual(includes("abc,abcde,ABC","ABC"),True)
00204     self.assertEqual(includes("abc,abcde,ABC","AbC",False),False)
00205     self.assertEqual(includes(["abc","abcde","AbC"],"ABC"),True)
00206     self.assertEqual(includes(["abc","abcde","AbC"],"ABC",False),False)
00207 
00208 
00209   def test_isAbsolutePath(self):
00210     self.assertEqual(isAbsolutePath("/usr/loca/bin"), True, "Result failed.")
00211     self.assertEqual(isAbsolutePath("c:\\"), True, "Result failed.")
00212     self.assertEqual(isAbsolutePath("\\\\localhost"), True, "Result failed.")
00213     # failed case
00214     # self.assertEqual(isAbsolutePath("\\localhost"), True, "Result failed.")
00215 
00216 
00217   def test_isURL(self):
00218     self.assertEqual(isURL("http://www.google.co.jp"), True, "Result failed.")
00219 
00220     # failed case
00221     # self.assertEqual(isURL("www.google.co.jp"), True, "Result failed.")
00222     # self.assertEqual(isURL("http:://www.google.co.jp"), True, "Result failed.")
00223 
00224 
00225   def test_otos(self):
00226     self.assertEqual(otos(123), "123", "Result failed.")
00227     self.assertEqual(otos("123"), "123", "Result failed.")
00228     self.assertEqual(otos(123456789123456789), "123456789123456789", "Result failed.")
00229     self.assertEqual(otos(0.123), "0.123", "Result failed.")
00230 
00231   
00232   def test_stringTo(self):
00233     int_ = [0]
00234     long_ = [0]
00235     float_ = [0.0]
00236     list_ = [[0.0,0.0,0.0,0.0]]
00237     str_ = [""]
00238     stringTo(int_,"123")
00239     stringTo(long_,"123")
00240     stringTo(float_,"0.123")
00241     stringTo(list_,"0,1.1,2.2,3.3")
00242     stringTo(str_,"hoge")
00243     print list_[0]
00244     self.assertEqual(int_[0], 123, "Result failed.")
00245     self.assertEqual(long_[0], 123, "Result failed.")
00246     self.assertEqual(float_[0], 0.123, "Result failed.")
00247     self.assertEqual(list_[0], [0,1.1,2.2,3.3], "Result failed.")
00248     self.assertEqual(str_[0], "hoge", "Result failed.")
00249 
00250     # failed case
00251     # self.assertEqual(stringTo("int",0.123), 0.123, "Result failed.")
00252 
00253 
00254   def test_unique_sv(self):
00255     sv = ["test0","test1","test0","test2","test0"]
00256     sv_ret = unique_sv(sv)
00257     self.assertEqual(sv_ret[0], "test0", "Result failed.")
00258     self.assertEqual(sv_ret[1], "test1", "Result failed.")
00259     self.assertEqual(sv_ret[2], "test2", "Result failed.")
00260     self.assertEqual(len(sv_ret), 3, "Result failed.")
00261 
00262     # failed case:  len(sv_ret) is 4
00263     # 
00264     # sv = ["test0","test1","test0","test2","TEST0"]
00265     # sv_ret = unique_sv(sv)
00266     # self.assertEqual(len(sv_ret), 3, "Result failed.")
00267 
00268     
00269   def test_flatten(self):
00270     sv = ["test0","test1","test2"]
00271     sv_ret = flatten(sv)
00272     self.assertEqual(sv_ret, "test0, test1, test2","Result failed.")
00273 
00274     # failed case:  Space is included.
00275     # self.assertEqual(sv_ret, "test0,test1,test2","Result failed.")
00276 
00277 
00278   def test_toArgv(self):
00279     pass
00280 
00281   
00282 
00283 ############### test #################
00284 if __name__ == '__main__':
00285         unittest.main()


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28