slice_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "fmt"
  17. "testing"
  18. . "github.com/smartystreets/goconvey/convey"
  19. )
  20. func TestAppendStr(t *testing.T) {
  21. Convey("Append a string to a slice with no duplicates", t, func() {
  22. s := []string{"a"}
  23. Convey("Append a string that does not exist in slice", func() {
  24. s = AppendStr(s, "b")
  25. So(len(s), ShouldEqual, 2)
  26. })
  27. Convey("Append a string that does exist in slice", func() {
  28. s = AppendStr(s, "b")
  29. So(len(s), ShouldEqual, 2)
  30. })
  31. })
  32. }
  33. func TestCompareSliceStr(t *testing.T) {
  34. Convey("Compares two 'string' type slices with elements and order", t, func() {
  35. Convey("Compare two slices that do have same elements and order", func() {
  36. So(CompareSliceStr(
  37. []string{"1", "2", "3"}, []string{"1", "2", "3"}), ShouldBeTrue)
  38. })
  39. Convey("Compare two slices that do have same elements but does not have same order", func() {
  40. So(!CompareSliceStr(
  41. []string{"2", "1", "3"}, []string{"1", "2", "3"}), ShouldBeTrue)
  42. })
  43. Convey("Compare two slices that have different number of elements", func() {
  44. So(!CompareSliceStr(
  45. []string{"2", "1"}, []string{"1", "2", "3"}), ShouldBeTrue)
  46. })
  47. })
  48. }
  49. func TestCompareSliceStrU(t *testing.T) {
  50. Convey("Compare two 'string' type slices with elements and ignore the order", t, func() {
  51. Convey("Compare two slices that do have same elements and order", func() {
  52. So(CompareSliceStrU(
  53. []string{"1", "2", "3"}, []string{"1", "2", "3"}), ShouldBeTrue)
  54. })
  55. Convey("Compare two slices that do have same elements but does not have same order", func() {
  56. So(CompareSliceStrU(
  57. []string{"2", "1", "3"}, []string{"1", "2", "3"}), ShouldBeTrue)
  58. })
  59. Convey("Compare two slices that have different number of elements", func() {
  60. So(!CompareSliceStrU(
  61. []string{"2", "1"}, []string{"1", "2", "3"}), ShouldBeTrue)
  62. })
  63. })
  64. }
  65. func BenchmarkAppendStr(b *testing.B) {
  66. s := []string{"a"}
  67. for i := 0; i < b.N; i++ {
  68. s = AppendStr(s, fmt.Sprint(b.N%3))
  69. }
  70. }
  71. func BenchmarkCompareSliceStr(b *testing.B) {
  72. s1 := []string{"1", "2", "3"}
  73. s2 := []string{"1", "2", "3"}
  74. for i := 0; i < b.N; i++ {
  75. CompareSliceStr(s1, s2)
  76. }
  77. }
  78. func BenchmarkCompareSliceStrU(b *testing.B) {
  79. s1 := []string{"1", "4", "2", "3"}
  80. s2 := []string{"1", "2", "3", "4"}
  81. for i := 0; i < b.N; i++ {
  82. CompareSliceStrU(s1, s2)
  83. }
  84. }