stringutil_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package stringutil
  10. import (
  11. "bytes"
  12. "fmt"
  13. "regexp"
  14. "sync"
  15. "testing"
  16. )
  17. func TestLongestCommonPrefix(t *testing.T) {
  18. if res := LongestCommonPrefix([]string{}); res != "" {
  19. t.Error("Unexpected result:", res)
  20. return
  21. }
  22. if res := LongestCommonPrefix([]string{"test"}); res != "test" {
  23. t.Error("Unexpected result:", res)
  24. return
  25. }
  26. if res := LongestCommonPrefix([]string{"tester", "test"}); res != "test" {
  27. t.Error("Unexpected result:", res)
  28. return
  29. }
  30. if res := LongestCommonPrefix([]string{"foo", "test"}); res != "" {
  31. t.Error("Unexpected result:", res)
  32. return
  33. }
  34. if res := LongestCommonPrefix([]string{"foo", "test"}); res != "" {
  35. t.Error("Unexpected result:", res)
  36. return
  37. }
  38. if res := LongestCommonPrefix([]string{"foo2", "foo1", "footest"}); res != "foo" {
  39. t.Error("Unexpected result:", res)
  40. return
  41. }
  42. }
  43. func TestPrintStringTable(t *testing.T) {
  44. if res := PrintStringTable(nil, 0); res != "" {
  45. t.Error("Unexpected result:\n", "#\n"+res+"#")
  46. return
  47. }
  48. test1 := []string{"foo", "bar", "tester", "1", "xxx", "test", "te"}
  49. if res := PrintStringTable(test1, 4); res != `
  50. foo bar tester 1
  51. xxx test te
  52. `[1:] {
  53. t.Error("Unexpected result:\n", "#"+res+"#")
  54. return
  55. }
  56. if res := PrintStringTable(test1, 3); res != `
  57. foo bar tester
  58. 1 xxx test
  59. te
  60. `[1:] {
  61. t.Error("Unexpected result:\n", "#"+res+"#")
  62. return
  63. }
  64. }
  65. func TestRuneSlice(t *testing.T) {
  66. sl := StringToRuneSlice("test")
  67. if fmt.Sprint(sl) != "[116 101 115 116]" {
  68. t.Error("Unexpected result:", sl)
  69. return
  70. }
  71. if RuneSliceToString(sl) != "test" {
  72. t.Error("Unexpected result:", sl)
  73. return
  74. }
  75. }
  76. func TestPluralCompareByteArray(t *testing.T) {
  77. if fmt.Sprintf("There are 2 test%s", Plural(2)) != "There are 2 tests" {
  78. t.Error("2 items should have an 's'")
  79. return
  80. }
  81. if fmt.Sprintf("There is 1 test%s", Plural(1)) != "There is 1 test" {
  82. t.Error("1 item should have no 's'")
  83. return
  84. }
  85. if fmt.Sprintf("There are 0 test%s", Plural(0)) != "There are 0 tests" {
  86. t.Error("0 items should have an 's'")
  87. return
  88. }
  89. }
  90. func TestGlobToRegex(t *testing.T) {
  91. globMatch(t, true, "*", "^$", "foo", "bar")
  92. globMatch(t, true, "?", "?", "^", "[", "]", "$")
  93. globMatch(t, true, "foo*", "foo", "food", "fool")
  94. globMatch(t, true, "f*d", "fud", "food")
  95. globMatch(t, true, "*d", "good", "bad")
  96. globMatch(t, true, "\\*\\?\\[\\{\\\\", "*?[{\\")
  97. globMatch(t, true, "[]^-]", "]", "-", "^")
  98. globMatch(t, true, "]", "]")
  99. globMatch(t, true, "^.$()|+", "^.$()|+")
  100. globMatch(t, true, "[^^]", ".", "$", "[", "]")
  101. globMatch(t, false, "[^^]", "^")
  102. globMatch(t, true, "[!!-]", "^", "?")
  103. globMatch(t, false, "[!!-]", "!", "-")
  104. globMatch(t, true, "{[12]*,[45]*,[78]*}", "1", "2!", "4", "42", "7", "7$")
  105. globMatch(t, false, "{[12]*,[45]*,[78]*}", "3", "6", "9ß")
  106. globMatch(t, true, "}", "}")
  107. globMatch(t, true, "abc,", "abc,")
  108. globMatch(t, true, "myfile[^9]", "myfile1")
  109. globMatch(t, true, "myfile[!9]", "myfile1")
  110. globMatch(t, false, "myfile[^9]", "myfile9")
  111. globMatch(t, false, "myfile[!9]", "myfile9")
  112. globMatch(t, true, "*.*", "tester/bla.txt")
  113. globMatch(t, false, "*.tmp", "tester/bla.txt")
  114. testdata := []string{"foo*test", "f?t", "*d", "all"}
  115. expected := []string{"foo", "f", "", "all"}
  116. for i, str := range testdata {
  117. res := GlobStartingLiterals(str)
  118. if res != expected[i] {
  119. t.Error("Unexpected starting literal for glob:", res, "str:",
  120. str, "expected:", expected[i])
  121. }
  122. }
  123. testdata = []string{"[", "{", "\\", "*.*\\", "[["}
  124. expected = []string{"Unclosed character class at 1 of [",
  125. "Unclosed group at 1 of {",
  126. "Missing escaped character at 1 of \\",
  127. "Missing escaped character at 4 of *.*\\",
  128. "Unclosed character class at 1 of [["}
  129. for i, str := range testdata {
  130. _, err := GlobToRegex(str)
  131. if err.Error() != expected[i] {
  132. t.Error("Unexpected error for glob:", err, "str:",
  133. str, "expected error:", expected[i])
  134. }
  135. }
  136. if str, err := GlobToRegex("[][]"); str != "[][]" || err != nil {
  137. t.Error("Unecpected glob parsing result:", str, err)
  138. }
  139. if str, err := GlobToRegex(")"); str != "\\)" || err != nil {
  140. t.Error("Unecpected glob parsing result:", str, err)
  141. }
  142. }
  143. func globMatch(t *testing.T, expectedResult bool, glob string, testStrings ...string) {
  144. re, err := GlobToRegex(glob)
  145. if err != nil {
  146. t.Error("Glob parsing error:", err)
  147. }
  148. for _, testString := range testStrings {
  149. res, err := regexp.MatchString(re, testString)
  150. if err != nil {
  151. t.Error("Regexp", re, "parsing error:", err, "from glob", glob)
  152. }
  153. if res != expectedResult {
  154. t.Error("Unexpected evaluation result. Glob:", glob, "testString:",
  155. testString, "expectedResult:", expectedResult)
  156. }
  157. }
  158. }
  159. func TestLevenshteinDistance(t *testing.T) {
  160. testdata1 := []string{"", "a", "", "abc", "", "a", "abc", "a", "b", "ac",
  161. "abcdefg", "a", "ab", "example", "sturgeon", "levenshtein", "distance"}
  162. testdata2 := []string{"", "", "a", "", "abc", "a", "abc", "ab", "ab", "abc",
  163. "xabxcdxxefxgx", "b", "ac", "samples", "urgently", "frankenstein", "difference"}
  164. expected := []int{0, 1, 1, 3, 3, 0, 0, 1, 1, 1, 6, 1, 1,
  165. 3, 6, 6, 5}
  166. for i, str1 := range testdata1 {
  167. res := LevenshteinDistance(str1, testdata2[i])
  168. if res != expected[i] {
  169. t.Error("Unexpected Levenshtein distance result:", res, "str1:",
  170. str1, "str2:", testdata2[i], "expected:", expected[i])
  171. }
  172. }
  173. }
  174. func TestVersionStringCompare(t *testing.T) {
  175. testdata1 := []string{"1", "1.1", "1.1", "2.1", "5.4.3.2.1", "1.674.2.18",
  176. "1.674.2", "1.674.2.5", "2.4.18.14smp", "2.4.18.15smp", "1.2.3a1",
  177. "2.18.15smp"}
  178. testdata2 := []string{"2", "2.0", "1.1", "2.0", "6.5.4.3.2", "1.674.2.5",
  179. "1.674.2.5", "1.674.2", "2.4.18.14smp", "2.4.18.14smp", "1.2.3b1",
  180. "2.4.18.14smp"}
  181. expected := []int{-1, -1, 0, 1, -1, 1, -1, 1, 0, 1, -1, 1}
  182. for i, str1 := range testdata1 {
  183. res := VersionStringCompare(str1, testdata2[i])
  184. if res != expected[i] {
  185. t.Error("Unexpected version string compare result:", res, "str1:",
  186. str1, "str2:", testdata2[i])
  187. }
  188. }
  189. }
  190. func TestVersionStringPartCompare(t *testing.T) {
  191. testdata1 := []string{"", "", "1", "1", "a", "1a", "a", "1a", "1a", "1", "12a", "12a1",
  192. "12a1"}
  193. testdata2 := []string{"", "1", "", "2", "b", "b", "2b", "2b", "1", "1b", "12b", "12a2",
  194. "12b1"}
  195. expected := []int{0, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1}
  196. for i, str1 := range testdata1 {
  197. res := versionStringPartCompare(str1, testdata2[i])
  198. if res != expected[i] {
  199. t.Error("Unexpected version string compare result:", res, "str1:",
  200. str1, "str2:", testdata2[i])
  201. }
  202. }
  203. }
  204. func TestIsAlphaNumeric(t *testing.T) {
  205. testdata := []string{"test", "123test", "test1234_123", "test#", "test-"}
  206. expected := []bool{true, true, true, false, false}
  207. for i, str := range testdata {
  208. if IsAlphaNumeric(str) != expected[i] {
  209. t.Error("Unexpected result for alphanumeric test:", str)
  210. }
  211. }
  212. }
  213. func TestIsTrueValue(t *testing.T) {
  214. testdata := []string{"1", "ok", "1", "FaLse", "0"}
  215. expected := []bool{true, true, true, false, false}
  216. for i, str := range testdata {
  217. if IsTrueValue(str) != expected[i] {
  218. t.Error("Unexpected result for alphanumeric test:", str)
  219. }
  220. }
  221. }
  222. func TestIndexOf(t *testing.T) {
  223. slice := []string{"foo", "bar", "test"}
  224. if res := IndexOf("foo", slice); res != 0 {
  225. t.Error("Unexpected result", res)
  226. return
  227. }
  228. if res := IndexOf("bar", slice); res != 1 {
  229. t.Error("Unexpected result", res)
  230. return
  231. }
  232. if res := IndexOf("test", slice); res != 2 {
  233. t.Error("Unexpected result", res)
  234. return
  235. }
  236. if res := IndexOf("hans", slice); res != -1 {
  237. t.Error("Unexpected result", res)
  238. return
  239. }
  240. }
  241. func TestMapKeys(t *testing.T) {
  242. testMap := map[string]interface{}{
  243. "1": "2",
  244. "3": "4",
  245. "5": "6",
  246. }
  247. if res := MapKeys(testMap); fmt.Sprint(res) != "[1 3 5]" {
  248. t.Error("Unexpected result:", res)
  249. return
  250. }
  251. }
  252. func TestGenerateRollingString(t *testing.T) {
  253. testdata := []string{"_-=-_", "abc", "=", ""}
  254. testlen := []int{20, 4, 5, 100}
  255. expected := []string{"_-=-__-=-__-=-__-=-_", "abca", "=====", ""}
  256. for i, str := range testdata {
  257. res := GenerateRollingString(str, testlen[i])
  258. if res != expected[i] {
  259. t.Error("Unexpected result for creating a roling string from:", str,
  260. "result:", res, "expected:", expected[i])
  261. }
  262. }
  263. }
  264. func TestConvertToString(t *testing.T) {
  265. if res := ConvertToString(""); res != "" {
  266. t.Error("Unexpected result:", res)
  267. return
  268. }
  269. if res := ConvertToString("test"); res != "test" {
  270. t.Error("Unexpected result:", res)
  271. return
  272. }
  273. if res := ConvertToString(4.123); res != "4.123" {
  274. t.Error("Unexpected result:", res)
  275. return
  276. }
  277. if res := ConvertToString(6); res != "6" {
  278. t.Error("Unexpected result:", res)
  279. return
  280. }
  281. if res := ConvertToString(map[string]int{"z": 1, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":1}` {
  282. t.Error("Unexpected result:", res)
  283. return
  284. }
  285. if res := ConvertToString([]int{1, 2, 3}); res != "[1,2,3]" {
  286. t.Error("Unexpected result:", res)
  287. return
  288. }
  289. if res := ConvertToString(map[interface{}]interface{}{"z": 1, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":1}` {
  290. t.Error("Unexpected result:", res)
  291. return
  292. }
  293. if res := ConvertToString(map[interface{}]interface{}{"z": []interface{}{1, 2, 3}, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":[1,2,3]}` {
  294. t.Error("Unexpected result:", res)
  295. return
  296. }
  297. if res := ConvertToString([]interface{}{1, sync.Mutex{}, 3}); res != `[1,{},3]` {
  298. t.Error("Unexpected result:", res)
  299. return
  300. }
  301. if res := ConvertToString([]interface{}{1, map[interface{}]interface{}{1: 2}, 3}); res != `[1,{"1":2},3]` {
  302. t.Error("Unexpected result:", res)
  303. return
  304. }
  305. if res := ConvertToString(&bytes.Buffer{}); res != "" {
  306. t.Error("Unexpected result:", res)
  307. return
  308. }
  309. // Not much to do with such a construct but we shouldn't fail!
  310. type foo struct{ i int }
  311. x := make(map[foo]foo)
  312. x[foo{1}] = foo{2}
  313. if res := ConvertToString(x); res != "map[{1}:{2}]" {
  314. t.Error("Unexpected result:", res)
  315. return
  316. }
  317. }
  318. func TestMD5HexString(t *testing.T) {
  319. res := MD5HexString("This is a test")
  320. if res != "ce114e4501d2f4e2dcea3e17b546f339" {
  321. t.Error("Unexpected md5 hex result", res)
  322. }
  323. }
  324. func TestLengthConstantEquals(t *testing.T) {
  325. if !LengthConstantEquals([]byte("test1"), []byte("test1")) {
  326. t.Error("Unexpected result")
  327. return
  328. }
  329. if LengthConstantEquals([]byte("test1"), []byte("test2")) {
  330. t.Error("Unexpected result")
  331. return
  332. }
  333. if LengthConstantEquals([]byte("test1"), []byte("test2test123")) {
  334. t.Error("Unexpected result")
  335. return
  336. }
  337. }
  338. func TestPrintGraphicStringTable(t *testing.T) {
  339. if res := PrintGraphicStringTable(nil, 0, 5, nil); res != "" {
  340. t.Error("Unexpected result:\n", "#\n"+res+"#")
  341. return
  342. }
  343. if res := PrintGraphicStringTable([]string{}, 4, 5, SingleLineTable); res != `
  344. ┌┐
  345. └┘
  346. `[1:] {
  347. t.Error("Unexpected result:\n", "#\n"+res+"#")
  348. return
  349. }
  350. if res := PrintCSVTable([]string{}, 4); res != "" {
  351. t.Error("Unexpected result:\n", "#\n"+res+"#")
  352. return
  353. }
  354. test1 := []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo",
  355. "bar", "tester", "1"}
  356. if res := PrintGraphicStringTable(test1, 4, 5, SingleLineTable); res != `
  357. ┌────┬───────┬───────┬────┐
  358. │foo │bar │tester │1 │
  359. │xxx │test │te │foo │
  360. │bar │tester │1 │ │
  361. └────┴───────┴───────┴────┘
  362. `[1:] {
  363. t.Error("Unexpected result:\n", "#\n"+res+"#")
  364. return
  365. }
  366. if res := PrintCSVTable(test1, 4); res != `
  367. foo, bar, tester, 1
  368. xxx, test, te, foo
  369. bar, tester, 1
  370. `[1:] {
  371. t.Error("Unexpected result:\n", "#\n"+res+"#")
  372. return
  373. }
  374. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo",
  375. "bar"}
  376. if res := PrintGraphicStringTable(test1, 4, 5, nil); res != `
  377. #########################
  378. #foo #bar #tester #1 #
  379. #xxx #test #te #foo #
  380. #bar # # # #
  381. #########################
  382. `[1:] {
  383. t.Error("Unexpected result:\n", "#\n"+res+"#")
  384. return
  385. }
  386. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  387. if res := PrintGraphicStringTable(test1, 4, 5, nil); res != `
  388. #########################
  389. #foo #bar #tester #1 #
  390. #xxx #test #te #foo #
  391. #########################
  392. `[1:] {
  393. t.Error("Unexpected result:\n", "#\n"+res+"#")
  394. return
  395. }
  396. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  397. if res := PrintGraphicStringTable(test1, 1, 2, SingleLineTable); res != `
  398. ┌───────┐
  399. │foo │
  400. │bar │
  401. ├───────┤
  402. │tester │
  403. │1 │
  404. │xxx │
  405. │test │
  406. │te │
  407. │foo │
  408. └───────┘
  409. `[1:] {
  410. t.Error("Unexpected result:\n", "#\n"+res+"#")
  411. return
  412. }
  413. if res := PrintCSVTable(test1, 1); res != `
  414. foo
  415. bar
  416. tester
  417. 1
  418. xxx
  419. test
  420. te
  421. foo
  422. `[1:] {
  423. t.Error("Unexpected result:\n", "#\n"+res+"#")
  424. return
  425. }
  426. if res := PrintGraphicStringTable(test1, 100, 0, nil); res != `
  427. ##########################################
  428. #foo #bar #tester #1 #xxx #test #te #foo #
  429. ##########################################
  430. `[1:] {
  431. t.Error("Unexpected result:\n", "#\n"+res+"#")
  432. return
  433. }
  434. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  435. if res := PrintGraphicStringTable(test1, 4, 5, SingleLineTable); res != `
  436. ┌────┬─────┬───────┬────┐
  437. │foo │bar │tester │1 │
  438. │xxx │test │te │foo │
  439. └────┴─────┴───────┴────┘
  440. `[1:] {
  441. t.Error("Unexpected result:\n", "#\n"+res+"#")
  442. return
  443. }
  444. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  445. if res := PrintGraphicStringTable(test1, 1, 2, SingleDoubleLineTable); res != `
  446. ╒═══════╕
  447. │foo │
  448. │bar │
  449. ╞═══════╡
  450. │tester │
  451. │1 │
  452. │xxx │
  453. │test │
  454. │te │
  455. │foo │
  456. ╘═══════╛
  457. `[1:] {
  458. t.Error("Unexpected result:\n", "#\n"+res+"#")
  459. return
  460. }
  461. if res := PrintGraphicStringTable(test1, 1, 2, DoubleSingleLineTable); res != `
  462. ╓───────╖
  463. ║foo ║
  464. ║bar ║
  465. ╟───────╢
  466. ║tester ║
  467. ║1 ║
  468. ║xxx ║
  469. ║test ║
  470. ║te ║
  471. ║foo ║
  472. ╙───────╜
  473. `[1:] {
  474. t.Error("Unexpected result:\n", "#\n"+res+"#")
  475. return
  476. }
  477. if res := PrintGraphicStringTable(test1, 1, 2, DoubleLineTable); res != `
  478. ╔═══════╗
  479. ║foo ║
  480. ║bar ║
  481. ╠═══════╣
  482. ║tester ║
  483. ║1 ║
  484. ║xxx ║
  485. ║test ║
  486. ║te ║
  487. ║foo ║
  488. ╚═══════╝
  489. `[1:] {
  490. t.Error("Unexpected result:\n", "#\n"+res+"#")
  491. return
  492. }
  493. if res := PrintGraphicStringTable(test1, 100, 0, SingleLineTable); res != `
  494. ┌────┬────┬───────┬──┬────┬─────┬───┬────┐
  495. │foo │bar │tester │1 │xxx │test │te │foo │
  496. └────┴────┴───────┴──┴────┴─────┴───┴────┘
  497. `[1:] {
  498. t.Error("Unexpected result:\n", "#\n"+res+"#")
  499. return
  500. }
  501. }