test_utf8_comparisons.vim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. " Tests for case-insensitive UTF-8 comparisons (utf_strnicmp() in mbyte.c)
  2. " Also test "g~ap".
  3. func Ch(a, op, b, expected)
  4. call assert_equal(eval(printf('"%s" %s "%s"', a:a, a:op, a:b)), a:expected,
  5. \ printf('"%s" %s "%s" should return %d', a:a, a:op, a:b, a:expected))
  6. endfunc
  7. func Chk(a, b, result)
  8. if a:result == 0
  9. call Ch(a:a, '==?', a:b, 1)
  10. call Ch(a:a, '!=?', a:b, 0)
  11. call Ch(a:a, '<=?', a:b, 1)
  12. call Ch(a:a, '>=?', a:b, 1)
  13. call Ch(a:a, '<?', a:b, 0)
  14. call Ch(a:a, '>?', a:b, 0)
  15. elseif a:result > 0
  16. call Ch(a:a, '==?', a:b, 0)
  17. call Ch(a:a, '!=?', a:b, 1)
  18. call Ch(a:a, '<=?', a:b, 0)
  19. call Ch(a:a, '>=?', a:b, 1)
  20. call Ch(a:a, '<?', a:b, 0)
  21. call Ch(a:a, '>?', a:b, 1)
  22. else
  23. call Ch(a:a, '==?', a:b, 0)
  24. call Ch(a:a, '!=?', a:b, 1)
  25. call Ch(a:a, '<=?', a:b, 1)
  26. call Ch(a:a, '>=?', a:b, 0)
  27. call Ch(a:a, '<?', a:b, 1)
  28. call Ch(a:a, '>?', a:b, 0)
  29. endif
  30. endfunc
  31. func Check(a, b, result)
  32. call Chk(a:a, a:b, a:result)
  33. call Chk(a:b, a:a, -a:result)
  34. endfunc
  35. func LT(a, b)
  36. call Check(a:a, a:b, -1)
  37. endfunc
  38. func GT(a, b)
  39. call Check(a:a, a:b, 1)
  40. endfunc
  41. func EQ(a, b)
  42. call Check(a:a, a:b, 0)
  43. endfunc
  44. func Test_comparisons()
  45. call EQ('', '')
  46. call LT('', 'a')
  47. call EQ('abc', 'abc')
  48. call EQ('Abc', 'abC')
  49. call LT('ab', 'abc')
  50. call LT('AB', 'abc')
  51. call LT('ab', 'aBc')
  52. call EQ('\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd', '\xd0\xb9\xd0\xa6\xd0\xa3\xd0\xba\xd0\x95\xd0\xbd')
  53. call LT('\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd', '\xd0\xaf\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd')
  54. call EQ('\xe2\x84\xaa', 'k')
  55. call LT('\xe2\x84\xaa', 'kkkkkk')
  56. call EQ('\xe2\x84\xaa\xe2\x84\xaa\xe2\x84\xaa', 'kkk')
  57. call LT('kk', '\xe2\x84\xaa\xe2\x84\xaa\xe2\x84\xaa')
  58. call EQ('\xe2\x84\xaa\xe2\x84\xa6k\xe2\x84\xaak\xcf\x89', 'k\xcf\x89\xe2\x84\xaakk\xe2\x84\xa6')
  59. call EQ('Abc\x80', 'AbC\x80')
  60. call LT('Abc\x80', 'AbC\x81')
  61. call LT('Abc', 'AbC\x80')
  62. call LT('abc\x80DEF', 'abc\x80def') " case folding stops at the first bad character
  63. call LT('\xc3XYZ', '\xc3xyz')
  64. call EQ('\xef\xbc\xba', '\xef\xbd\x9a') " FF3A (upper), FF5A (lower)
  65. call GT('\xef\xbc\xba', '\xef\xbc\xff') " first string is ok and equals \xef\xbd\x9a after folding, second string is illegal and was left unchanged, then the strings were bytewise compared
  66. call LT('\xc3', '\xc3\x83')
  67. call EQ('\xc3\xa3xYz', '\xc3\x83XyZ')
  68. for n in range(0x60, 0xFF)
  69. call LT(printf('xYz\x%.2X', n-1), printf('XyZ\x%.2X', n))
  70. endfor
  71. for n in range(0x80, 0xBF)
  72. call EQ(printf('xYz\xc2\x%.2XUvW', n), printf('XyZ\xc2\x%.2XuVw', n))
  73. endfor
  74. for n in range(0xC0, 0xFF)
  75. call LT(printf('xYz\xc2\x%.2XUvW', n), printf('XyZ\xc2\x%.2XuVw', n))
  76. endfor
  77. endfunc
  78. " test that g~ap changes one paragraph only.
  79. func Test_gap()
  80. new
  81. " setup text
  82. call feedkeys("iabcd\<cr>\<cr>defg", "tx")
  83. " modify only first line
  84. call feedkeys("gg0g~ap", "tx")
  85. call assert_equal(["ABCD", "", "defg"], getline(1,3))
  86. endfunc
  87. " test that g~, ~ and gU correctly upper-cases ß
  88. func Test_uppercase_sharp_ss()
  89. new
  90. call setline(1, repeat(['ß'], 4))
  91. call cursor(1, 1)
  92. norm! ~
  93. call assert_equal('ẞ', getline(line('.')))
  94. norm! ~
  95. call assert_equal('ß', getline(line('.')))
  96. call cursor(2, 1)
  97. norm! g~l
  98. call assert_equal('ẞ', getline(line('.')))
  99. norm! g~l
  100. call assert_equal('ß', getline(line('.')))
  101. call cursor(3, 1)
  102. norm! gUl
  103. call assert_equal('ẞ', getline(line('.')))
  104. norm! vgU
  105. call assert_equal('ẞ', getline(line('.')))
  106. norm! vgu
  107. call assert_equal('ß', getline(line('.')))
  108. norm! gul
  109. call assert_equal('ß', getline(line('.')))
  110. call cursor(4, 1)
  111. norm! vgU
  112. call assert_equal('ẞ', getline(line('.')))
  113. norm! vgu
  114. call assert_equal('ß', getline(line('.')))
  115. bw!
  116. endfunc
  117. " vim: shiftwidth=2 sts=2 expandtab