width_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package liner
  2. import (
  3. "strconv"
  4. "testing"
  5. )
  6. func accent(in []rune) []rune {
  7. var out []rune
  8. for _, r := range in {
  9. out = append(out, r)
  10. out = append(out, '\u0301')
  11. }
  12. return out
  13. }
  14. type testCase struct {
  15. s []rune
  16. glyphs int
  17. }
  18. var testCases = []testCase{
  19. {[]rune("query"), 5},
  20. {[]rune("私"), 2},
  21. {[]rune("hello世界"), 9},
  22. }
  23. func TestCountGlyphs(t *testing.T) {
  24. for _, testCase := range testCases {
  25. count := countGlyphs(testCase.s)
  26. if count != testCase.glyphs {
  27. t.Errorf("ASCII count incorrect. %d != %d", count, testCase.glyphs)
  28. }
  29. count = countGlyphs(accent(testCase.s))
  30. if count != testCase.glyphs {
  31. t.Errorf("Accent count incorrect. %d != %d", count, testCase.glyphs)
  32. }
  33. }
  34. }
  35. func compare(a, b []rune, name string, t *testing.T) {
  36. if len(a) != len(b) {
  37. t.Errorf(`"%s" != "%s" in %s"`, string(a), string(b), name)
  38. return
  39. }
  40. for i := range a {
  41. if a[i] != b[i] {
  42. t.Errorf(`"%s" != "%s" in %s"`, string(a), string(b), name)
  43. return
  44. }
  45. }
  46. }
  47. func TestPrefixGlyphs(t *testing.T) {
  48. for _, testCase := range testCases {
  49. for i := 0; i <= len(testCase.s); i++ {
  50. iter := strconv.Itoa(i)
  51. out := getPrefixGlyphs(testCase.s, i)
  52. compare(out, testCase.s[:i], "ascii prefix "+iter, t)
  53. out = getPrefixGlyphs(accent(testCase.s), i)
  54. compare(out, accent(testCase.s[:i]), "accent prefix "+iter, t)
  55. }
  56. out := getPrefixGlyphs(testCase.s, 999)
  57. compare(out, testCase.s, "ascii prefix overflow", t)
  58. out = getPrefixGlyphs(accent(testCase.s), 999)
  59. compare(out, accent(testCase.s), "accent prefix overflow", t)
  60. out = getPrefixGlyphs(testCase.s, -3)
  61. if len(out) != 0 {
  62. t.Error("ascii prefix negative")
  63. }
  64. out = getPrefixGlyphs(accent(testCase.s), -3)
  65. if len(out) != 0 {
  66. t.Error("accent prefix negative")
  67. }
  68. }
  69. }
  70. func TestSuffixGlyphs(t *testing.T) {
  71. for _, testCase := range testCases {
  72. for i := 0; i <= len(testCase.s); i++ {
  73. iter := strconv.Itoa(i)
  74. out := getSuffixGlyphs(testCase.s, i)
  75. compare(out, testCase.s[len(testCase.s)-i:], "ascii suffix "+iter, t)
  76. out = getSuffixGlyphs(accent(testCase.s), i)
  77. compare(out, accent(testCase.s[len(testCase.s)-i:]), "accent suffix "+iter, t)
  78. }
  79. out := getSuffixGlyphs(testCase.s, 999)
  80. compare(out, testCase.s, "ascii suffix overflow", t)
  81. out = getSuffixGlyphs(accent(testCase.s), 999)
  82. compare(out, accent(testCase.s), "accent suffix overflow", t)
  83. out = getSuffixGlyphs(testCase.s, -3)
  84. if len(out) != 0 {
  85. t.Error("ascii suffix negative")
  86. }
  87. out = getSuffixGlyphs(accent(testCase.s), -3)
  88. if len(out) != 0 {
  89. t.Error("accent suffix negative")
  90. }
  91. }
  92. }