ring_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 appsrv
  15. import (
  16. "testing"
  17. )
  18. func TestRing(t *testing.T) {
  19. var (
  20. r = NewRing(10)
  21. push = func(v int32) {
  22. r.Push(v)
  23. }
  24. pop = func(want int32) {
  25. got := r.Pop().(int32)
  26. if got != want {
  27. t.Fatalf("got %d, want %d", got, want)
  28. }
  29. for i := r.header; i != r.tail; i = nextPointer(i, len(r.buffer)) {
  30. if r.buffer[i] != nil {
  31. t.Fatalf("head %d, tail %d, index %d not nil",
  32. r.header, r.tail, i)
  33. }
  34. }
  35. }
  36. )
  37. push(10)
  38. push(20)
  39. push(30)
  40. pop(10)
  41. pop(20)
  42. pop(30)
  43. if v := r.Pop(); v != nil {
  44. t.Fatalf("want nil, got %#v", v)
  45. }
  46. }
  47. func TestOverflow(t *testing.T) {
  48. r := NewRing(1)
  49. if r.Capacity() != 1 {
  50. t.Error("Wrong capacity")
  51. }
  52. if r.Size() != 0 {
  53. t.Error("Wrong size")
  54. }
  55. if r.Push(1) != true {
  56. t.Error("Push should success")
  57. }
  58. if r.Push(2) != false {
  59. t.Error("Push should fail")
  60. }
  61. r.Pop()
  62. r.Push(2)
  63. if r.Size() != 1 {
  64. t.Error("Wrong size")
  65. }
  66. r.Pop()
  67. if r.Size() != 0 {
  68. t.Error("Wrong size")
  69. }
  70. r.Push(3)
  71. if r.Size() != 1 {
  72. t.Error("Wrong size")
  73. }
  74. }