You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package utils
  4. import (
  5. "strings"
  6. "testing"
  7. )
  8. const (
  9. monteCristo = `Both the count and Baptistin had told the truth when they announced to Morcerf the proposed visit of the major, which had served Monte Cristo as a pretext for declining Albert's invitation. Seven o'clock had just struck, and M. Bertuccio, according to the command which had been given him, had two hours before left for Auteuil, when a cab stopped at the door, and after depositing its occupant at the gate, immediately hurried away, as if ashamed of its employment. The visitor was about fifty-two years of age, dressed in one of the green surtouts, ornamented with black frogs, which have so long maintained their popularity all over Europe. He wore trousers of blue cloth, boots tolerably clean, but not of the brightest polish, and a little too thick in the soles, buckskin gloves, a hat somewhat resembling in shape those usually worn by the gendarmes, and a black cravat striped with white, which, if the proprietor had not worn it of his own free will, might have passed for a halter, so much did it resemble one. Such was the picturesque costume of the person who rang at the gate, and demanded if it was not at No. 30 in the Avenue des Champs-Elysees that the Count of Monte Cristo lived, and who, being answered by the porter in the affirmative, entered, closed the gate after him, and began to ascend the steps.`
  10. )
  11. func TestTokenLineBuilder(t *testing.T) {
  12. lineLen := 400
  13. var tl TokenLineBuilder
  14. tl.Initialize(lineLen, " ")
  15. for _, token := range strings.Fields(monteCristo) {
  16. tl.Add(token)
  17. }
  18. lines := tl.Lines()
  19. if len(lines) != 4 {
  20. t.Errorf("expected 4 lines, got %d", len(lines))
  21. }
  22. for _, line := range lines {
  23. if len(line) > lineLen {
  24. t.Errorf("line length %d exceeds maximum of %d", len(line), lineLen)
  25. }
  26. }
  27. joined := strings.Join(lines, " ")
  28. if joined != monteCristo {
  29. t.Errorf("text incorrectly split into lines: %s instead of %s", joined, monteCristo)
  30. }
  31. }
  32. func TestBuildTokenLines(t *testing.T) {
  33. val := BuildTokenLines(512, []string{"a", "b", "c"}, ",")
  34. assertEqual(val, []string{"a,b,c"}, t)
  35. val = BuildTokenLines(10, []string{"abcd", "efgh", "ijkl"}, ",")
  36. assertEqual(val, []string{"abcd,efgh", "ijkl"}, t)
  37. }
  38. func TestTLBuilderAddParts(t *testing.T) {
  39. var tl TokenLineBuilder
  40. tl.Initialize(20, " ")
  41. tl.Add("bob")
  42. tl.AddParts("@", "alice")
  43. tl.AddParts("@", "ErgoBot__")
  44. assertEqual(tl.Lines(), []string{"bob @alice", "@ErgoBot__"}, t)
  45. }
  46. func BenchmarkTokenLines(b *testing.B) {
  47. tokens := strings.Fields(monteCristo)
  48. b.ResetTimer()
  49. for i := 0; i < b.N; i++ {
  50. var tl TokenLineBuilder
  51. tl.Initialize(400, " ")
  52. for _, tok := range tokens {
  53. tl.Add(tok)
  54. }
  55. tl.Lines()
  56. }
  57. }