3 * @version $Id: utf8_position.test.php,v 1.1 2006/09/30 22:49:43 harryf Exp $
8 //--------------------------------------------------------------------
14 require_once(dirname(__FILE__).'/../config.php');
15 require_once UTF8 . '/utils/position.php';
17 //--------------------------------------------------------------------
22 class test_utf8_position extends UnitTestCase {
24 function test_ascii_char_to_byte() {
26 $this->assertIdentical(utf8_byte_position($str, 3), 3);
27 $this->assertIdentical(utf8_byte_position($str, 3, 4), array(3,4));
28 $this->assertIdentical(utf8_byte_position($str, -1), 0);
29 $this->assertIdentical(utf8_byte_position($str, 8), 7);
32 function test_multibyte_char_to_byte() {
33 $str = 'Iñtërnâtiônàlizætiøn';
34 $this->assertIdentical(utf8_byte_position($str, 3), 4);
35 $this->assertIdentical(utf8_byte_position($str, 3, 5), array(4,7));
36 $this->assertIdentical(utf8_byte_position($str, -1), 0);
37 $this->assertIdentical(utf8_byte_position($str, 28), 27);
40 // tests for utf8_locate_current_chr & utf8_locate_next_chr
41 function test_singlebyte(){
44 // single byte, should return current index
45 $tests[] = array('aaживπά우리をあöä',0,0);
46 $tests[] = array('aaживπά우리をあöä',1,1);
48 foreach($tests as $test){
49 $this->assertIdentical(utf8_locate_current_chr($test[0],$test[1]),$test[2]);
53 $tests[] = array('aaживπά우리をあöä',1,1);
55 foreach($tests as $test){
56 $this->assertIdentical(utf8_locate_next_chr($test[0],$test[1]),$test[2]);
61 function test_twobyte(){
62 // two byte, should move to boundary, expect even number
64 $tests[] = array('aaживπά우리をあöä',2,2);
65 $tests[] = array('aaживπά우리をあöä',3,2);
66 $tests[] = array('aaживπά우리をあöä',4,4);
68 foreach($tests as $test){
69 $this->assertIdentical(utf8_locate_current_chr($test[0],$test[1]),$test[2]);
73 $tests[] = array('aaживπά우리をあöä',2,2);
74 $tests[] = array('aaживπά우리をあöä',3,4);
75 $tests[] = array('aaживπά우리をあöä',4,4);
77 foreach($tests as $test){
78 $this->assertIdentical(utf8_locate_next_chr($test[0],$test[1]),$test[2]);
82 function test_threebyte(){
83 // three byte, should move to boundary 10 or 13
85 $tests[] = array('aaживπά우리をあöä',10,10);
86 $tests[] = array('aaживπά우리をあöä',11,10);
87 $tests[] = array('aaживπά우리をあöä',12,10);
88 $tests[] = array('aaживπά우리をあöä',13,13);
90 foreach($tests as $test){
91 $this->assertIdentical(utf8_locate_current_chr($test[0],$test[1]),$test[2]);
95 $tests[] = array('aaживπά우리をあöä',10,10);
96 $tests[] = array('aaживπά우리をあöä',11,13);
97 $tests[] = array('aaживπά우리をあöä',12,13);
98 $tests[] = array('aaживπά우리をあöä',13,13);
100 foreach($tests as $test){
101 $this->assertIdentical(utf8_locate_next_chr($test[0],$test[1]),$test[2]);
105 function test_bounds(){
108 $tests[] = array('aaживπά우리をあöä',-2,0);
109 $tests[] = array('aaживπά우리をあöä',128,29);
111 foreach($tests as $test){
112 $this->assertIdentical(utf8_locate_current_chr($test[0],$test[1]),$test[2]);
115 $tests[] = array('aaживπά우리をあöä',-2,0);
116 $tests[] = array('aaживπά우리をあöä',128,29);
118 foreach($tests as $test){
119 $this->assertIdentical(utf8_locate_next_chr($test[0],$test[1]),$test[2]);
126 //--------------------------------------------------------------------
131 if (!defined('TEST_RUNNING')) {
132 define('TEST_RUNNING', true);
133 $test = &new test_utf8_position();
134 $reporter = & getTestReporter();
135 $test->run($reporter);