rev 593 - in trunk: pr/test src

SVN User <[email protected]> Thu, 10 Jun 2004 01:32:37 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-06-10 01:32:33 -0400 (Thu, 10 Jun 2004)
New Revision: 593

Modified:
   trunk/pr/test/test.pr
   trunk/src/builtins-string.c
Log:
fixed string.find()

Modified: trunk/pr/test/test.pr
===================================================================
--- trunk/pr/test/test.pr	2004-06-10 05:20:18 UTC (rev 592)
+++ trunk/pr/test/test.pr	2004-06-10 05:32:33 UTC (rev 593)
@@ -1,36 +1,109 @@
 #!/usr/bin/env prothon
 
-def err(n):
-	print 'test',n,'failed'
-	Sys.exit(1)
+# strmod.pr
 
-x = "abcdefghijklm"
+# TODO:
+#  *) More comprehensive tests, especially replace() and split().
+#  *) Test binary string handling (when parser is fixed).
 
-if "c:bc:acegi:mlkjih" !=  x[2]+':'+x[1:3]+':'+x[0:10:2]+':'+x[12:6:-1]:
-	err(1)
+failures = 0
 
-x = "abcdef"
+s = "test %4d..%4.2e..%s" % (-12, 100/3, 'jhgdjhg')
 
-if "e:de:def:ab:fed:fedcba:abcdef" !=  x[-2]+':'+x[-3:-1]+':'+x[3:]+':'+x[:2]+':'+x[:2:-1]+':'+x[::-1]+':'+x[:]:
-	err(2)
+if s != 'test  -12..3.33e+01..jhgdjhg': 
+	print 'error 1'
+	print s
+	failures = failures+1
+	
+if "abCDefG".lower() != "abcdefg":
+	print "lower() failed!"
+	failures = failures+1
 
-if 3 !=  Len("abc"):  err(3)
+if "abCDefG345%^&".lower() != "abcdefg345%^&":
+	print "lower() failed!"
+	failures = failures+1
 
-if "ABC" !=  "abc".upper(): err(4)
+if "abCDefG".upper() != "ABCDEFG":
+	print "upper() failed!"
+	failures = failures+1
 
-if "abc" !=  "ABC".lower(): err(5)
+if "+_098abCDefG".upper() != "+_098ABCDEFG":
+	print "upper() failed!"
+	failures = failures+1
 
-if "Abc def" !=  "abc def".capitalize(): err(6)
+if "abCDefG".replace("CDe","456") != "ab456fG":
+	print "replace() failed!"
+	failures = failures+1
 
-if "Abc Def" !=  "abc def".capWords(): err(7)
+if "abcdefg".capitalize() != "Abcdefg":
+	print "capitalize() failed!"
+	failures = failures+1
 
-if "<x >" != '<'+" x ".lStrip()+'>': err(8)
+if "a\tb\tcde".expandTabs() != "a        b        cde":
+	print "expandtabs() failed!"
+	failures = failures+1
 
-if "< x>" !=  '<'+" x ".rStrip()+'>': err(9)
+if " \t \r\n abc".lStrip() != "abc":
+	print "lstrip() failed!"
+	failures = failures+1
 
-if "<x>" !=  '<'+" x ".strip()+'>': err(10)
+if "abc \t \r\n ".rStrip() != "abc":
+	print "rstrip() failed!"
+	failures = failures+1
 
-print '\nall tests passed\n'
+if " \t \r\n abc \t \r\n ".strip() != "abc":
+	print "strip() failed!"
+	failures = failures+1
 
+if ".a.bc.def.ghij.".split('.') != ['', 'a', 'bc', 'def', 'ghij', '']:
+	print "split() failed!"
+	failures = failures+1
 
+if "a.bc.def.ghij".split('.') != ['a', 'bc', 'def', 'ghij']:
+	print "split() failed!"
+	failures = failures+1
 
+if "a.bc.def.ghij.".split('.') != ['a', 'bc', 'def', 'ghij', '']:
+	print "split() failed!"
+	failures = failures+1
+
+if "..a.bc.def.ghij.".split('.') != ['', '', 'a', 'bc', 'def', 'ghij', '']:
+	print "split() failed!"
+	failures = failures+1
+
+if "a.bc.def.ghij".split('a') != ['', '.bc.def.ghij']:
+	print "split() failed!"
+	failures = failures+1
+
+if "a.ab.abc.abcd".split('a') != ['', '.', 'b.', 'bc.', 'bcd']:
+	print "split() failed!"
+	failures = failures+1
+
+if ".a.ab.abc.abcd".split('a') != ['.', '.', 'b.', 'bc.', 'bcd']:
+	print "split() failed!"
+	failures = failures+1
+
+if ".a.ab.abc.abcd.".split('a') != ['.', '.', 'b.', 'bc.', 'bcd.']:
+	print "split() failed!"
+	failures = failures+1
+
+if "aaaaabaacaadaaaaabaacaad".split("aab") != ["aaa","aacaadaaa","aacaad"]:
+	print "split() failed!"
+	failures = failures+1
+
+if "aaaaabaacaadaaaaabaacaad".find("aab",0) != 3:
+	print "split() failed!"
+	failures = failures+1
+
+s = "%d" % 3333
+
+if s != '3333': 
+	print 'error 2'
+	print s
+	failures = failures+1
+
+if failures==0:
+	print '\nall tests passed\n'
+else:
+	print "%d failure(s) occurred!\n"%failures
+	Sys.exit(1)

Modified: trunk/src/builtins-string.c
===================================================================
--- trunk/src/builtins-string.c	2004-06-10 05:20:18 UTC (rev 592)
+++ trunk/src/builtins-string.c	2004-06-10 05:32:33 UTC (rev 593)
@@ -505,16 +505,15 @@
 	return changeCase(ist,self,&toupper,AT_WORD_START);
 }
 
-#define int_imm_value(objid)	(objid->data.i64)
 DEF(String, find, FPARM2( stringToFind, NULL, indexToStartLooking, NEW_INT(0) )) {
-	size_t str_len = pr_strlen(self);
+	size_t str_len;
 	size_t find_len = 0;
-	char *found, *find_ptr = 0;
+	char *found, *find_ptr = 0, *self_str;
 	size_t start = 0;
 	obj_p result = 0;
 
 	BIN_CONTENT_CHK(String);
-
+	str_len = pr_strlen(self);
 	if (!has_proto(ist, parms[1], String_OBJ)) {
 		raise_exception(ist, OBJ(TYPE_EXC), "find function parameter 1 must be a string");
 		return NULL;
@@ -523,7 +522,7 @@
 		raise_exception(ist, OBJ(TYPE_EXC), "find function parameter 2 must be an integer");
 		return NULL;
 	} else {
-		start = (size_t)int_imm_value(parms[3]);
+		start = (size_t)int2i32t(ist, parms[3]);
 	}
 
 	find_ptr = pr2c_strptr(ist, parms[1]);
@@ -531,13 +530,15 @@
 	if (find_len<1) {
 		return new_int_obj(ist,0);
 	}
-	found = bin_strstr(pr2c_strptr(ist, self)+int_imm_value(parms[3]),str_len,find_ptr,find_len);
+	self_str = pr2c_strptr(ist, self);
+	found = bin_strstr(self_str+start,str_len,find_ptr,find_len);
 	if (found) {
-		result = new_int_obj(ist,found-pr2c_strptr(ist, self));
+		result = new_int_obj(ist,found-self_str);
 	} else {
 		result = new_int_obj(ist,0);
 	}
-
+	pr_free(find_ptr);
+	pr_free(self_str);
 	return result;
 }