days_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain 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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package validate
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestDaysCheck(t *testing.T) {
  21. tcs := []struct {
  22. input_days []int
  23. input_max int
  24. input_min int
  25. want []int
  26. want_error error
  27. }{
  28. {
  29. []int{3, 5, 1, 4, 8, 3, 4},
  30. 10,
  31. 1,
  32. []int{1, 3, 3, 4, 4, 5, 8},
  33. fmt.Errorf("Has repeat day %v", []int{1, 3, 3, 4, 4, 5, 8}),
  34. },
  35. {
  36. []int{10, 1},
  37. 10,
  38. 1,
  39. []int{1, 10},
  40. nil,
  41. },
  42. {
  43. []int{10, 3, 5},
  44. 3,
  45. 5,
  46. []int{3, 5, 10},
  47. fmt.Errorf("Out of range"),
  48. },
  49. }
  50. for _, tc := range tcs {
  51. days, err := DaysCheck(tc.input_days, tc.input_min, tc.input_max)
  52. if !reflect.DeepEqual(days, tc.want) {
  53. t.Fatalf("want: %v, actual: %v", tc.want, days)
  54. }
  55. if !reflect.DeepEqual(err, tc.want_error) {
  56. t.Fatal("err not correct")
  57. }
  58. }
  59. }