string_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2013 com authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package com
  15. import (
  16. "testing"
  17. )
  18. func TestIsLetter(t *testing.T) {
  19. if IsLetter('1') {
  20. t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", false, true)
  21. }
  22. if IsLetter('[') {
  23. t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", false, true)
  24. }
  25. if !IsLetter('a') {
  26. t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", true, false)
  27. }
  28. if !IsLetter('Z') {
  29. t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", true, false)
  30. }
  31. }
  32. func TestExpand(t *testing.T) {
  33. match := map[string]string{
  34. "domain": "gowalker.org",
  35. "subdomain": "github.com",
  36. }
  37. s := "http://{domain}/{subdomain}/{0}/{1}"
  38. sR := "http://gowalker.org/github.com/Unknwon/gowalker"
  39. if Expand(s, match, "Unknwon", "gowalker") != sR {
  40. t.Errorf("Expand:\n Expect => %s\n Got => %s\n", sR, s)
  41. }
  42. }
  43. func TestReverse(t *testing.T) {
  44. if Reverse("abcdefg") != "gfedcba" {
  45. t.Errorf("Reverse:\n Except => %s\n Got =>%s\n", "gfedcba", Reverse("abcdefg"))
  46. }
  47. if Reverse("上善若水厚德载物") != "物载德厚水若善上" {
  48. t.Errorf("Reverse:\n Except => %s\n Got =>%s\n", "物载德厚水若善上", Reverse("上善若水厚德载物"))
  49. }
  50. }
  51. func BenchmarkIsLetter(b *testing.B) {
  52. for i := 0; i < b.N; i++ {
  53. IsLetter('a')
  54. }
  55. }
  56. func BenchmarkExpand(b *testing.B) {
  57. match := map[string]string{
  58. "domain": "gowalker.org",
  59. "subdomain": "github.com",
  60. }
  61. s := "http://{domain}/{subdomain}/{0}/{1}"
  62. for i := 0; i < b.N; i++ {
  63. Expand(s, match, "Unknwon", "gowalker")
  64. }
  65. }
  66. func BenchmarkReverse(b *testing.B) {
  67. s := "abscef中文"
  68. for i := 0; i < b.N; i++ {
  69. Reverse(s)
  70. }
  71. }