string_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. . "github.com/smartystreets/goconvey/convey"
  18. "crypto/rand"
  19. "bytes"
  20. )
  21. func TestAESEncrypt(t *testing.T) {
  22. t.Parallel()
  23. key := make([]byte, 16) // AES-128
  24. _, err := rand.Read(key)
  25. if err != nil {
  26. t.Fatal("Failed to create 128 bit AES key: " + err.Error())
  27. }
  28. plaintext := []byte("this will be encrypted")
  29. _, err = AESGCMEncrypt(key, plaintext)
  30. if err != nil {
  31. t.Fatal("Failed to encrypt plaintext: " + err.Error())
  32. }
  33. }
  34. func TestAESDecrypt(t *testing.T) {
  35. t.Parallel()
  36. key := make([]byte, 16) // AES-128
  37. _, err := rand.Read(key)
  38. if err != nil {
  39. t.Fatal("Failed to create 128 bit AES key: " + err.Error())
  40. }
  41. plaintext := []byte("this will be encrypted")
  42. ciphertext, err := AESGCMEncrypt(key, plaintext)
  43. if err != nil {
  44. t.Fatal("Failed to encrypt plaintext: " + err.Error())
  45. }
  46. decrypted, err := AESGCMDecrypt(key, ciphertext)
  47. if err != nil {
  48. t.Fatal("Failed to decrypt ciphertext: " + err.Error())
  49. }
  50. if bytes.Compare(decrypted, plaintext) != 0 {
  51. t.Fatal("Decryption was not performed correctly")
  52. }
  53. }
  54. func TestIsLetter(t *testing.T) {
  55. if IsLetter('1') {
  56. t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", false, true)
  57. }
  58. if IsLetter('[') {
  59. t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", false, true)
  60. }
  61. if !IsLetter('a') {
  62. t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", true, false)
  63. }
  64. if !IsLetter('Z') {
  65. t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", true, false)
  66. }
  67. }
  68. //func TestExpand(t *testing.T) {
  69. // match := map[string]string{
  70. // "domain": "gowalker.org",
  71. // "subdomain": "github.com",
  72. // }
  73. // s := "http://{domain}/{subdomain}/{0}/{1}"
  74. // sR := "http://notabug.org/makenotabuggreatagain/gowalker"
  75. // if Expand(s, match, "Unknwon", "gowalker") != sR {
  76. // t.Errorf("Expand:\n Expect => %s\n Got => %s\n", sR, s)
  77. // }
  78. //}
  79. func TestReverse(t *testing.T) {
  80. if Reverse("abcdefg") != "gfedcba" {
  81. t.Errorf("Reverse:\n Except => %s\n Got =>%s\n", "gfedcba", Reverse("abcdefg"))
  82. }
  83. if Reverse("上善若水厚德载物") != "物载德厚水若善上" {
  84. t.Errorf("Reverse:\n Except => %s\n Got =>%s\n", "物载德厚水若善上", Reverse("上善若水厚德载物"))
  85. }
  86. }
  87. func Test_ToSnakeCase(t *testing.T) {
  88. cases := map[string]string{
  89. "HTTPServer": "http_server",
  90. "_camelCase": "_camel_case",
  91. "NoHTTPS": "no_https",
  92. "Wi_thF": "wi_th_f",
  93. "_AnotherTES_TCaseP": "_another_tes_t_case_p",
  94. "ALL": "all",
  95. "_HELLO_WORLD_": "_hello_world_",
  96. "HELLO_WORLD": "hello_world",
  97. "HELLO____WORLD": "hello____world",
  98. "TW": "tw",
  99. "_C": "_c",
  100. " sentence case ": "__sentence_case__",
  101. " Mixed-hyphen case _and SENTENCE_case and UPPER-case": "_mixed_hyphen_case__and_sentence_case_and_upper_case",
  102. }
  103. Convey("Convert string into snake case", t, func() {
  104. for old, new := range cases {
  105. So(ToSnakeCase(old), ShouldEqual, new)
  106. }
  107. })
  108. }
  109. func BenchmarkIsLetter(b *testing.B) {
  110. for i := 0; i < b.N; i++ {
  111. IsLetter('a')
  112. }
  113. }
  114. func BenchmarkExpand(b *testing.B) {
  115. match := map[string]string{
  116. "domain": "gowalker.org",
  117. "subdomain": "github.com",
  118. }
  119. s := "http://{domain}/{subdomain}/{0}/{1}"
  120. for i := 0; i < b.N; i++ {
  121. Expand(s, match, "Unknwon", "gowalker")
  122. }
  123. }
  124. func BenchmarkReverse(b *testing.B) {
  125. s := "abscef中文"
  126. for i := 0; i < b.N; i++ {
  127. Reverse(s)
  128. }
  129. }