used_ring_internal_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package virtqueue
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestUsedRing_MemoryLayout(t *testing.T) {
  7. const queueSize = 2
  8. memory := make([]byte, usedRingSize(queueSize))
  9. r := newUsedRing(queueSize, memory)
  10. *r.flags = 0x01ff
  11. *r.ringIndex = 1
  12. r.ring[0] = UsedElement{
  13. DescriptorIndex: 0x0123,
  14. Length: 0x4567,
  15. }
  16. r.ring[1] = UsedElement{
  17. DescriptorIndex: 0x89ab,
  18. Length: 0xcdef,
  19. }
  20. assert.Equal(t, []byte{
  21. 0xff, 0x01,
  22. 0x01, 0x00,
  23. 0x23, 0x01, 0x00, 0x00,
  24. 0x67, 0x45, 0x00, 0x00,
  25. 0xab, 0x89, 0x00, 0x00,
  26. 0xef, 0xcd, 0x00, 0x00,
  27. 0x00, 0x00,
  28. }, memory)
  29. }
  30. //func TestUsedRing_Take(t *testing.T) {
  31. // const queueSize = 8
  32. //
  33. // tests := []struct {
  34. // name string
  35. // ring []UsedElement
  36. // ringIndex uint16
  37. // lastIndex uint16
  38. // expected []UsedElement
  39. // }{
  40. // {
  41. // name: "nothing new",
  42. // ring: []UsedElement{
  43. // {DescriptorIndex: 1},
  44. // {DescriptorIndex: 2},
  45. // {DescriptorIndex: 3},
  46. // {DescriptorIndex: 4},
  47. // {},
  48. // {},
  49. // {},
  50. // {},
  51. // },
  52. // ringIndex: 4,
  53. // lastIndex: 4,
  54. // expected: nil,
  55. // },
  56. // {
  57. // name: "no overflow",
  58. // ring: []UsedElement{
  59. // {DescriptorIndex: 1},
  60. // {DescriptorIndex: 2},
  61. // {DescriptorIndex: 3},
  62. // {DescriptorIndex: 4},
  63. // {},
  64. // {},
  65. // {},
  66. // {},
  67. // },
  68. // ringIndex: 4,
  69. // lastIndex: 1,
  70. // expected: []UsedElement{
  71. // {DescriptorIndex: 2},
  72. // {DescriptorIndex: 3},
  73. // {DescriptorIndex: 4},
  74. // },
  75. // },
  76. // {
  77. // name: "ring overflow",
  78. // ring: []UsedElement{
  79. // {DescriptorIndex: 9},
  80. // {DescriptorIndex: 10},
  81. // {DescriptorIndex: 3},
  82. // {DescriptorIndex: 4},
  83. // {DescriptorIndex: 5},
  84. // {DescriptorIndex: 6},
  85. // {DescriptorIndex: 7},
  86. // {DescriptorIndex: 8},
  87. // },
  88. // ringIndex: 10,
  89. // lastIndex: 7,
  90. // expected: []UsedElement{
  91. // {DescriptorIndex: 8},
  92. // {DescriptorIndex: 9},
  93. // {DescriptorIndex: 10},
  94. // },
  95. // },
  96. // {
  97. // name: "index overflow",
  98. // ring: []UsedElement{
  99. // {DescriptorIndex: 9},
  100. // {DescriptorIndex: 10},
  101. // {DescriptorIndex: 3},
  102. // {DescriptorIndex: 4},
  103. // {DescriptorIndex: 5},
  104. // {DescriptorIndex: 6},
  105. // {DescriptorIndex: 7},
  106. // {DescriptorIndex: 8},
  107. // },
  108. // ringIndex: 2,
  109. // lastIndex: 65535,
  110. // expected: []UsedElement{
  111. // {DescriptorIndex: 8},
  112. // {DescriptorIndex: 9},
  113. // {DescriptorIndex: 10},
  114. // },
  115. // },
  116. // }
  117. // for _, tt := range tests {
  118. // t.Run(tt.name, func(t *testing.T) {
  119. // memory := make([]byte, usedRingSize(queueSize))
  120. // r := newUsedRing(queueSize, memory)
  121. //
  122. // copy(r.ring, tt.ring)
  123. // *r.ringIndex = tt.ringIndex
  124. // r.lastIndex = tt.lastIndex
  125. //
  126. // assert.Equal(t, tt.expected, r.take())
  127. // })
  128. // }
  129. //}