map_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package goja
  2. import (
  3. "hash/maphash"
  4. "math"
  5. "math/big"
  6. "strconv"
  7. "testing"
  8. )
  9. func testMapHashVal(v1, v2 Value, expected bool, t *testing.T) {
  10. var h maphash.Hash
  11. actual := v1.hash(&h) == v2.hash(&h)
  12. if actual != expected {
  13. t.Fatalf("testMapHashVal failed for %v, %v", v1, v2)
  14. }
  15. }
  16. func TestMapHash(t *testing.T) {
  17. testMapHashVal(_NaN, _NaN, true, t)
  18. testMapHashVal(valueTrue, valueFalse, false, t)
  19. testMapHashVal(valueTrue, valueTrue, true, t)
  20. testMapHashVal(intToValue(0), _negativeZero, true, t)
  21. testMapHashVal(asciiString("Test"), asciiString("Test"), true, t)
  22. testMapHashVal(newStringValue("Тест"), newStringValue("Тест"), true, t)
  23. testMapHashVal(floatToValue(1.2345), floatToValue(1.2345), true, t)
  24. testMapHashVal(SymIterator, SymToStringTag, false, t)
  25. testMapHashVal(SymIterator, SymIterator, true, t)
  26. testMapHashVal((*valueBigInt)(big.NewInt(1)), (*valueBigInt)(big.NewInt(-1)), false, t)
  27. testMapHashVal((*valueBigInt)(big.NewInt(1)), (*valueBigInt)(big.NewInt(1)), true, t)
  28. // The following tests introduce indeterministic behaviour
  29. //testMapHashVal(asciiString("Test"), asciiString("Test1"), false, t)
  30. //testMapHashVal(newStringValue("Тест"), asciiString("Test"), false, t)
  31. //testMapHashVal(newStringValue("Тест"), newStringValue("Тест1"), false, t)
  32. }
  33. func TestOrderedMap(t *testing.T) {
  34. m := newOrderedMap(&maphash.Hash{})
  35. for i := int64(0); i < 50; i++ {
  36. m.set(intToValue(i), asciiString(strconv.FormatInt(i, 10)))
  37. }
  38. if m.size != 50 {
  39. t.Fatalf("Unexpected size: %d", m.size)
  40. }
  41. for i := int64(0); i < 50; i++ {
  42. expected := asciiString(strconv.FormatInt(i, 10))
  43. actual := m.get(intToValue(i))
  44. if !expected.SameAs(actual) {
  45. t.Fatalf("Wrong value for %d", i)
  46. }
  47. }
  48. for i := int64(0); i < 50; i += 2 {
  49. if !m.remove(intToValue(i)) {
  50. t.Fatalf("remove(%d) return false", i)
  51. }
  52. }
  53. if m.size != 25 {
  54. t.Fatalf("Unexpected size: %d", m.size)
  55. }
  56. iter := m.newIter()
  57. count := 0
  58. for {
  59. entry := iter.next()
  60. if entry == nil {
  61. break
  62. }
  63. m.remove(entry.key)
  64. count++
  65. }
  66. if count != 25 {
  67. t.Fatalf("Unexpected iter count: %d", count)
  68. }
  69. if m.size != 0 {
  70. t.Fatalf("Unexpected size: %d", m.size)
  71. }
  72. }
  73. func TestOrderedMapCollision(t *testing.T) {
  74. m := newOrderedMap(&maphash.Hash{})
  75. n1 := uint64(123456789)
  76. n2 := math.Float64frombits(n1)
  77. n1Key := intToValue(int64(n1))
  78. n2Key := floatToValue(n2)
  79. m.set(n1Key, asciiString("n1"))
  80. m.set(n2Key, asciiString("n2"))
  81. if m.size == len(m.hashTable) {
  82. t.Fatal("Expected a collision but there wasn't one")
  83. }
  84. if n2Val := m.get(n2Key); !asciiString("n2").SameAs(n2Val) {
  85. t.Fatalf("unexpected n2Val: %v", n2Val)
  86. }
  87. if n1Val := m.get(n1Key); !asciiString("n1").SameAs(n1Val) {
  88. t.Fatalf("unexpected nVal: %v", n1Val)
  89. }
  90. if !m.remove(n1Key) {
  91. t.Fatal("removing n1Key returned false")
  92. }
  93. if n2Val := m.get(n2Key); !asciiString("n2").SameAs(n2Val) {
  94. t.Fatalf("2: unexpected n2Val: %v", n2Val)
  95. }
  96. }
  97. func TestOrderedMapIter(t *testing.T) {
  98. m := newOrderedMap(&maphash.Hash{})
  99. iter := m.newIter()
  100. ent := iter.next()
  101. if ent != nil {
  102. t.Fatal("entry should be nil")
  103. }
  104. iter1 := m.newIter()
  105. m.set(intToValue(1), valueTrue)
  106. ent = iter.next()
  107. if ent != nil {
  108. t.Fatal("2: entry should be nil")
  109. }
  110. ent = iter1.next()
  111. if ent == nil {
  112. t.Fatal("entry is nil")
  113. }
  114. if !intToValue(1).SameAs(ent.key) {
  115. t.Fatal("unexpected key")
  116. }
  117. if !valueTrue.SameAs(ent.value) {
  118. t.Fatal("unexpected value")
  119. }
  120. }
  121. func TestOrderedMapIterVisitAfterReAdd(t *testing.T) {
  122. m := newOrderedMap(&maphash.Hash{})
  123. one := intToValue(1)
  124. two := intToValue(2)
  125. m.set(one, valueTrue)
  126. m.set(two, valueTrue)
  127. iter := m.newIter()
  128. entry := iter.next()
  129. if !one.SameAs(entry.key) {
  130. t.Fatalf("1: unexpected key: %v", entry.key)
  131. }
  132. if !m.remove(one) {
  133. t.Fatal("remove returned false")
  134. }
  135. entry = iter.next()
  136. if !two.SameAs(entry.key) {
  137. t.Fatalf("2: unexpected key: %v", entry.key)
  138. }
  139. m.set(one, valueTrue)
  140. entry = iter.next()
  141. if entry == nil {
  142. t.Fatal("entry is nil")
  143. }
  144. if !one.SameAs(entry.key) {
  145. t.Fatalf("3: unexpected key: %v", entry.key)
  146. }
  147. }
  148. func TestOrderedMapIterAddAfterClear(t *testing.T) {
  149. m := newOrderedMap(&maphash.Hash{})
  150. one := intToValue(1)
  151. m.set(one, valueTrue)
  152. iter := m.newIter()
  153. iter.next()
  154. m.clear()
  155. m.set(one, valueTrue)
  156. entry := iter.next()
  157. if entry == nil {
  158. t.Fatal("entry is nil")
  159. }
  160. if entry.key != one {
  161. t.Fatalf("unexpected key: %v", entry.key)
  162. }
  163. entry = iter.next()
  164. if entry != nil {
  165. t.Fatalf("entry is not nil: %v", entry)
  166. }
  167. }
  168. func TestOrderedMapIterDeleteCurrent(t *testing.T) {
  169. m := newOrderedMap(&maphash.Hash{})
  170. one := intToValue(1)
  171. two := intToValue(2)
  172. iter := m.newIter()
  173. m.set(one, valueTrue)
  174. m.set(two, valueTrue)
  175. entry := iter.next()
  176. if entry.key != one {
  177. t.Fatalf("unexpected key: %v", entry.key)
  178. }
  179. m.remove(one)
  180. entry = iter.next()
  181. if entry.key != two {
  182. t.Fatalf("2: unexpected key: %v", entry.key)
  183. }
  184. }