Fortran · 2347 bytes Raw Blame History
1 program test_utf8
2 use utf8_module
3 implicit none
4
5 character(len=:), allocatable :: test_str
6 integer :: char_count, byte_idx, char_idx, width
7
8 ! Test 1: ASCII string
9 test_str = "Hello"
10 char_count = utf8_char_count(test_str)
11 width = utf8_display_width(test_str)
12 print *, "Test 1: 'Hello'"
13 print *, " Bytes:", len_trim(test_str), "Chars:", char_count, "Width:", width
14 if (char_count /= 5 .or. width /= 5) then
15 print *, " FAILED!"
16 stop 1
17 else
18 print *, " PASSED"
19 end if
20
21 ! Test 2: Box drawing characters
22 test_str = "├──"
23 char_count = utf8_char_count(test_str)
24 width = utf8_display_width(test_str)
25 print *, "Test 2: '├──' (box chars)"
26 print *, " Bytes:", len_trim(test_str), "Chars:", char_count, "Width:", width
27 if (char_count /= 3 .or. width /= 3) then
28 print *, " FAILED! Expected 3 chars, 3 width"
29 stop 1
30 else
31 print *, " PASSED"
32 end if
33
34 ! Test 3: Mixed content
35 test_str = " ├── Tab 1"
36 char_count = utf8_char_count(test_str)
37 width = utf8_display_width(test_str)
38 print *, "Test 3: ' ├── Tab 1'"
39 print *, " Bytes:", len_trim(test_str), "Chars:", char_count, "Width:", width
40 if (char_count /= 13) then
41 print *, " FAILED! Expected 13 chars, got", char_count
42 stop 1
43 else
44 print *, " PASSED"
45 end if
46
47 ! Test 4: Character to byte index conversion
48 test_str = "├──"
49 byte_idx = utf8_char_to_byte_index(test_str, 2) ! 2nd character
50 print *, "Test 4: Char to byte index"
51 print *, " 2nd char in '├──' starts at byte:", byte_idx
52 if (byte_idx /= 4) then ! First char is 3 bytes, so 2nd starts at byte 4
53 print *, " FAILED! Expected byte 4, got", byte_idx
54 stop 1
55 else
56 print *, " PASSED"
57 end if
58
59 ! Test 5: Byte to character index conversion
60 test_str = "├──"
61 char_idx = utf8_byte_to_char_index(test_str, 4) ! Byte 4
62 print *, "Test 5: Byte to char index"
63 print *, " Byte 4 in '├──' is char:", char_idx
64 if (char_idx /= 2) then
65 print *, " FAILED! Expected char 2, got", char_idx
66 stop 1
67 else
68 print *, " PASSED"
69 end if
70
71 print *, ""
72 print *, "All UTF-8 tests passed!"
73
74 end program test_utf8
75