stringutil_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 TestStripCStyleComments(t *testing.T) {
  91. test := `
  92. // Comment1
  93. This is a test
  94. /* A
  95. comment
  96. // Comment2
  97. */ bla
  98. `
  99. if out := string(StripCStyleComments([]byte(test))); out != `
  100. This is a test
  101. bla
  102. ` {
  103. t.Error("Unexpected return:", out)
  104. return
  105. }
  106. }
  107. func TestGlobToRegex(t *testing.T) {
  108. globMatch(t, true, "*", "^$", "foo", "bar")
  109. globMatch(t, true, "?", "?", "^", "[", "]", "$")
  110. globMatch(t, true, "foo*", "foo", "food", "fool")
  111. globMatch(t, true, "f*d", "fud", "food")
  112. globMatch(t, true, "*d", "good", "bad")
  113. globMatch(t, true, "\\*\\?\\[\\{\\\\", "*?[{\\")
  114. globMatch(t, true, "[]^-]", "]", "-", "^")
  115. globMatch(t, true, "]", "]")
  116. globMatch(t, true, "^.$()|+", "^.$()|+")
  117. globMatch(t, true, "[^^]", ".", "$", "[", "]")
  118. globMatch(t, false, "[^^]", "^")
  119. globMatch(t, true, "[!!-]", "^", "?")
  120. globMatch(t, false, "[!!-]", "!", "-")
  121. globMatch(t, true, "{[12]*,[45]*,[78]*}", "1", "2!", "4", "42", "7", "7$")
  122. globMatch(t, false, "{[12]*,[45]*,[78]*}", "3", "6", "9ß")
  123. globMatch(t, true, "}", "}")
  124. globMatch(t, true, "abc,", "abc,")
  125. globMatch(t, true, "myfile[^9]", "myfile1")
  126. globMatch(t, true, "myfile[!9]", "myfile1")
  127. globMatch(t, false, "myfile[^9]", "myfile9")
  128. globMatch(t, false, "myfile[!9]", "myfile9")
  129. globMatch(t, true, "*.*", "tester/bla.txt")
  130. globMatch(t, false, "*.tmp", "tester/bla.txt")
  131. testdata := []string{"foo*test", "f?t", "*d", "all"}
  132. expected := []string{"foo", "f", "", "all"}
  133. for i, str := range testdata {
  134. res := GlobStartingLiterals(str)
  135. if res != expected[i] {
  136. t.Error("Unexpected starting literal for glob:", res, "str:",
  137. str, "expected:", expected[i])
  138. }
  139. }
  140. testdata = []string{"[", "{", "\\", "*.*\\", "[["}
  141. expected = []string{"Unclosed character class at 1 of [",
  142. "Unclosed group at 1 of {",
  143. "Missing escaped character at 1 of \\",
  144. "Missing escaped character at 4 of *.*\\",
  145. "Unclosed character class at 1 of [["}
  146. for i, str := range testdata {
  147. _, err := GlobToRegex(str)
  148. if err.Error() != expected[i] {
  149. t.Error("Unexpected error for glob:", err, "str:",
  150. str, "expected error:", expected[i])
  151. }
  152. }
  153. if str, err := GlobToRegex("[][]"); str != "[][]" || err != nil {
  154. t.Error("Unecpected glob parsing result:", str, err)
  155. }
  156. if str, err := GlobToRegex(")"); str != "\\)" || err != nil {
  157. t.Error("Unecpected glob parsing result:", str, err)
  158. }
  159. }
  160. func globMatch(t *testing.T, expectedResult bool, glob string, testStrings ...string) {
  161. re, err := GlobToRegex(glob)
  162. if err != nil {
  163. t.Error("Glob parsing error:", err)
  164. }
  165. for _, testString := range testStrings {
  166. res, err := regexp.MatchString(re, testString)
  167. if err != nil {
  168. t.Error("Regexp", re, "parsing error:", err, "from glob", glob)
  169. }
  170. if res != expectedResult {
  171. t.Error("Unexpected evaluation result. Glob:", glob, "testString:",
  172. testString, "expectedResult:", expectedResult)
  173. }
  174. }
  175. }
  176. func TestLevenshteinDistance(t *testing.T) {
  177. testdata1 := []string{"", "a", "", "abc", "", "a", "abc", "a", "b", "ac",
  178. "abcdefg", "a", "ab", "example", "sturgeon", "levenshtein", "distance"}
  179. testdata2 := []string{"", "", "a", "", "abc", "a", "abc", "ab", "ab", "abc",
  180. "xabxcdxxefxgx", "b", "ac", "samples", "urgently", "frankenstein", "difference"}
  181. expected := []int{0, 1, 1, 3, 3, 0, 0, 1, 1, 1, 6, 1, 1,
  182. 3, 6, 6, 5}
  183. for i, str1 := range testdata1 {
  184. res := LevenshteinDistance(str1, testdata2[i])
  185. if res != expected[i] {
  186. t.Error("Unexpected Levenshtein distance result:", res, "str1:",
  187. str1, "str2:", testdata2[i], "expected:", expected[i])
  188. }
  189. }
  190. }
  191. func TestVersionStringCompare(t *testing.T) {
  192. testdata1 := []string{"1", "1.1", "1.1", "2.1", "5.4.3.2.1", "1.674.2.18",
  193. "1.674.2", "1.674.2.5", "2.4.18.14smp", "2.4.18.15smp", "1.2.3a1",
  194. "2.18.15smp"}
  195. testdata2 := []string{"2", "2.0", "1.1", "2.0", "6.5.4.3.2", "1.674.2.5",
  196. "1.674.2.5", "1.674.2", "2.4.18.14smp", "2.4.18.14smp", "1.2.3b1",
  197. "2.4.18.14smp"}
  198. expected := []int{-1, -1, 0, 1, -1, 1, -1, 1, 0, 1, -1, 1}
  199. for i, str1 := range testdata1 {
  200. res := VersionStringCompare(str1, testdata2[i])
  201. if res != expected[i] {
  202. t.Error("Unexpected version string compare result:", res, "str1:",
  203. str1, "str2:", testdata2[i])
  204. }
  205. }
  206. }
  207. func TestVersionStringPartCompare(t *testing.T) {
  208. testdata1 := []string{"", "", "1", "1", "a", "1a", "a", "1a", "1a", "1", "12a", "12a1",
  209. "12a1"}
  210. testdata2 := []string{"", "1", "", "2", "b", "b", "2b", "2b", "1", "1b", "12b", "12a2",
  211. "12b1"}
  212. expected := []int{0, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1}
  213. for i, str1 := range testdata1 {
  214. res := versionStringPartCompare(str1, testdata2[i])
  215. if res != expected[i] {
  216. t.Error("Unexpected version string compare result:", res, "str1:",
  217. str1, "str2:", testdata2[i])
  218. }
  219. }
  220. }
  221. func TestIsAlphaNumeric(t *testing.T) {
  222. testdata := []string{"test", "123test", "test1234_123", "test#", "test-"}
  223. expected := []bool{true, true, true, false, false}
  224. for i, str := range testdata {
  225. if IsAlphaNumeric(str) != expected[i] {
  226. t.Error("Unexpected result for alphanumeric test:", str)
  227. }
  228. }
  229. }
  230. func TestIsTrueValue(t *testing.T) {
  231. testdata := []string{"1", "ok", "1", "FaLse", "0"}
  232. expected := []bool{true, true, true, false, false}
  233. for i, str := range testdata {
  234. if IsTrueValue(str) != expected[i] {
  235. t.Error("Unexpected result for alphanumeric test:", str)
  236. }
  237. }
  238. }
  239. func TestIndexOf(t *testing.T) {
  240. slice := []string{"foo", "bar", "test"}
  241. if res := IndexOf("foo", slice); res != 0 {
  242. t.Error("Unexpected result", res)
  243. return
  244. }
  245. if res := IndexOf("bar", slice); res != 1 {
  246. t.Error("Unexpected result", res)
  247. return
  248. }
  249. if res := IndexOf("test", slice); res != 2 {
  250. t.Error("Unexpected result", res)
  251. return
  252. }
  253. if res := IndexOf("hans", slice); res != -1 {
  254. t.Error("Unexpected result", res)
  255. return
  256. }
  257. }
  258. func TestMapKeys(t *testing.T) {
  259. testMap := map[string]interface{}{
  260. "1": "2",
  261. "3": "4",
  262. "5": "6",
  263. }
  264. if res := MapKeys(testMap); fmt.Sprint(res) != "[1 3 5]" {
  265. t.Error("Unexpected result:", res)
  266. return
  267. }
  268. }
  269. func TestCreateDisplayString(t *testing.T) {
  270. testdata := []string{"this is a tEST", "_bla", "a_bla", "a__bla", "a__b_la", "",
  271. "a fool a to be to"}
  272. expected := []string{"This Is a Test", "Bla", "A Bla", "A Bla", "A B La", "",
  273. "A Fool a to Be To"}
  274. for i, str := range testdata {
  275. res := CreateDisplayString(str)
  276. if res != expected[i] {
  277. t.Error("Unexpected result for creating a display string from:", str,
  278. "result:", res, "expected:", expected[i])
  279. }
  280. }
  281. }
  282. func TestGenerateRollingString(t *testing.T) {
  283. testdata := []string{"_-=-_", "abc", "=", ""}
  284. testlen := []int{20, 4, 5, 100}
  285. expected := []string{"_-=-__-=-__-=-__-=-_", "abca", "=====", ""}
  286. for i, str := range testdata {
  287. res := GenerateRollingString(str, testlen[i])
  288. if res != expected[i] {
  289. t.Error("Unexpected result for creating a roling string from:", str,
  290. "result:", res, "expected:", expected[i])
  291. }
  292. }
  293. }
  294. func TestConvertToString(t *testing.T) {
  295. if res := ConvertToString(""); res != "" {
  296. t.Error("Unexpected result:", res)
  297. return
  298. }
  299. if res := ConvertToString("test"); res != "test" {
  300. t.Error("Unexpected result:", res)
  301. return
  302. }
  303. if res := ConvertToString(4.123); res != "4.123" {
  304. t.Error("Unexpected result:", res)
  305. return
  306. }
  307. if res := ConvertToString(6); res != "6" {
  308. t.Error("Unexpected result:", res)
  309. return
  310. }
  311. if res := ConvertToString(map[string]int{"z": 1, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":1}` {
  312. t.Error("Unexpected result:", res)
  313. return
  314. }
  315. if res := ConvertToString([]int{1, 2, 3}); res != "[1,2,3]" {
  316. t.Error("Unexpected result:", res)
  317. return
  318. }
  319. if res := ConvertToString(map[interface{}]interface{}{"z": 1, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":1}` {
  320. t.Error("Unexpected result:", res)
  321. return
  322. }
  323. if res := ConvertToString(map[interface{}]interface{}{"z": []interface{}{1, 2, 3}, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":[1,2,3]}` {
  324. t.Error("Unexpected result:", res)
  325. return
  326. }
  327. if res := ConvertToString([]interface{}{1, sync.Mutex{}, 3}); res != `[1,{},3]` {
  328. t.Error("Unexpected result:", res)
  329. return
  330. }
  331. if res := ConvertToString([]interface{}{1, map[interface{}]interface{}{1: 2}, 3}); res != `[1,{"1":2},3]` {
  332. t.Error("Unexpected result:", res)
  333. return
  334. }
  335. if res := ConvertToString(&bytes.Buffer{}); res != "" {
  336. t.Error("Unexpected result:", res)
  337. return
  338. }
  339. // Not much to do with such a construct but we shouldn't fail!
  340. type foo struct{ i int }
  341. x := make(map[foo]foo)
  342. x[foo{1}] = foo{2}
  343. if res := ConvertToString(x); res != "map[{1}:{2}]" {
  344. t.Error("Unexpected result:", res)
  345. return
  346. }
  347. }
  348. func TestMD5HexString(t *testing.T) {
  349. res := MD5HexString("This is a test")
  350. if res != "ce114e4501d2f4e2dcea3e17b546f339" {
  351. t.Error("Unexpected md5 hex result", res)
  352. }
  353. }
  354. func TestLengthConstantEquals(t *testing.T) {
  355. if !LengthConstantEquals([]byte("test1"), []byte("test1")) {
  356. t.Error("Unexpected result")
  357. return
  358. }
  359. if LengthConstantEquals([]byte("test1"), []byte("test2")) {
  360. t.Error("Unexpected result")
  361. return
  362. }
  363. if LengthConstantEquals([]byte("test1"), []byte("test2test123")) {
  364. t.Error("Unexpected result")
  365. return
  366. }
  367. }
  368. func TestPrintGraphicStringTable(t *testing.T) {
  369. if res := PrintGraphicStringTable(nil, 0, 5, nil); res != "" {
  370. t.Error("Unexpected result:\n", "#\n"+res+"#")
  371. return
  372. }
  373. if res := PrintGraphicStringTable([]string{}, 4, 5, SingleLineTable); res != `
  374. ┌┐
  375. └┘
  376. `[1:] {
  377. t.Error("Unexpected result:\n", "#\n"+res+"#")
  378. return
  379. }
  380. if res := PrintCSVTable([]string{}, 4); res != "" {
  381. t.Error("Unexpected result:\n", "#\n"+res+"#")
  382. return
  383. }
  384. test1 := []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo",
  385. "bar", "tester", "1"}
  386. if res := PrintGraphicStringTable(test1, 4, 5, SingleLineTable); res != `
  387. ┌────┬───────┬───────┬────┐
  388. │foo │bar │tester │1 │
  389. │xxx │test │te │foo │
  390. │bar │tester │1 │ │
  391. └────┴───────┴───────┴────┘
  392. `[1:] {
  393. t.Error("Unexpected result:\n", "#\n"+res+"#")
  394. return
  395. }
  396. if res := PrintCSVTable(test1, 4); res != `
  397. foo, bar, tester, 1
  398. xxx, test, te, foo
  399. bar, tester, 1
  400. `[1:] {
  401. t.Error("Unexpected result:\n", "#\n"+res+"#")
  402. return
  403. }
  404. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo",
  405. "bar"}
  406. if res := PrintGraphicStringTable(test1, 4, 5, nil); res != `
  407. #########################
  408. #foo #bar #tester #1 #
  409. #xxx #test #te #foo #
  410. #bar # # # #
  411. #########################
  412. `[1:] {
  413. t.Error("Unexpected result:\n", "#\n"+res+"#")
  414. return
  415. }
  416. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  417. if res := PrintGraphicStringTable(test1, 4, 5, nil); res != `
  418. #########################
  419. #foo #bar #tester #1 #
  420. #xxx #test #te #foo #
  421. #########################
  422. `[1:] {
  423. t.Error("Unexpected result:\n", "#\n"+res+"#")
  424. return
  425. }
  426. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  427. if res := PrintGraphicStringTable(test1, 1, 2, SingleLineTable); res != `
  428. ┌───────┐
  429. │foo │
  430. │bar │
  431. ├───────┤
  432. │tester │
  433. │1 │
  434. │xxx │
  435. │test │
  436. │te │
  437. │foo │
  438. └───────┘
  439. `[1:] {
  440. t.Error("Unexpected result:\n", "#\n"+res+"#")
  441. return
  442. }
  443. if res := PrintCSVTable(test1, 1); res != `
  444. foo
  445. bar
  446. tester
  447. 1
  448. xxx
  449. test
  450. te
  451. foo
  452. `[1:] {
  453. t.Error("Unexpected result:\n", "#\n"+res+"#")
  454. return
  455. }
  456. if res := PrintGraphicStringTable(test1, 100, 0, nil); res != `
  457. ##########################################
  458. #foo #bar #tester #1 #xxx #test #te #foo #
  459. ##########################################
  460. `[1:] {
  461. t.Error("Unexpected result:\n", "#\n"+res+"#")
  462. return
  463. }
  464. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  465. if res := PrintGraphicStringTable(test1, 4, 5, SingleLineTable); res != `
  466. ┌────┬─────┬───────┬────┐
  467. │foo │bar │tester │1 │
  468. │xxx │test │te │foo │
  469. └────┴─────┴───────┴────┘
  470. `[1:] {
  471. t.Error("Unexpected result:\n", "#\n"+res+"#")
  472. return
  473. }
  474. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  475. if res := PrintGraphicStringTable(test1, 1, 2, SingleDoubleLineTable); res != `
  476. ╒═══════╕
  477. │foo │
  478. │bar │
  479. ╞═══════╡
  480. │tester │
  481. │1 │
  482. │xxx │
  483. │test │
  484. │te │
  485. │foo │
  486. ╘═══════╛
  487. `[1:] {
  488. t.Error("Unexpected result:\n", "#\n"+res+"#")
  489. return
  490. }
  491. if res := PrintGraphicStringTable(test1, 1, 2, DoubleSingleLineTable); res != `
  492. ╓───────╖
  493. ║foo ║
  494. ║bar ║
  495. ╟───────╢
  496. ║tester ║
  497. ║1 ║
  498. ║xxx ║
  499. ║test ║
  500. ║te ║
  501. ║foo ║
  502. ╙───────╜
  503. `[1:] {
  504. t.Error("Unexpected result:\n", "#\n"+res+"#")
  505. return
  506. }
  507. if res := PrintGraphicStringTable(test1, 1, 2, DoubleLineTable); res != `
  508. ╔═══════╗
  509. ║foo ║
  510. ║bar ║
  511. ╠═══════╣
  512. ║tester ║
  513. ║1 ║
  514. ║xxx ║
  515. ║test ║
  516. ║te ║
  517. ║foo ║
  518. ╚═══════╝
  519. `[1:] {
  520. t.Error("Unexpected result:\n", "#\n"+res+"#")
  521. return
  522. }
  523. if res := PrintGraphicStringTable(test1, 100, 0, SingleLineTable); res != `
  524. ┌────┬────┬───────┬──┬────┬─────┬───┬────┐
  525. │foo │bar │tester │1 │xxx │test │te │foo │
  526. └────┴────┴───────┴──┴────┴─────┴───┴────┘
  527. `[1:] {
  528. t.Error("Unexpected result:\n", "#\n"+res+"#")
  529. return
  530. }
  531. }