6 "github.com/bradfitz/iter"
7 qt "github.com/frankban/quicktest"
9 pp "github.com/anacrolix/torrent/peer_protocol"
12 func keysAsSlice(m map[Request]struct{}) (sl []Request) {
19 func makeTypicalRequests() map[Request]struct{} {
20 m := make(map[Request]struct{})
21 for p := pp.Integer(0); p < 4; p++ {
22 for c := pp.Integer(0); c < 16; c++ {
23 m[Request{p, ChunkSpec{c * defaultChunkSize, defaultChunkSize}}] = struct{}{}
29 func TestLogExampleRequestMapOrdering(t *testing.T) {
30 for k := range makeTypicalRequests() {
35 func TestRequestMapOrderingPersistent(t *testing.T) {
36 m := makeTypicalRequests()
37 // Shows that map order is persistent across separate range statements.
38 qt.Assert(t, keysAsSlice(m), qt.ContentEquals, keysAsSlice(m))
41 func TestRequestMapOrderAcrossInstances(t *testing.T) {
42 // This shows that different map instances with the same contents can have the same range order.
43 qt.Assert(t, keysAsSlice(makeTypicalRequests()), qt.ContentEquals, keysAsSlice(makeTypicalRequests()))
46 // Added for testing repeating loop iteration after shuffling in Peer.applyRequestState.
47 func TestForLoopRepeatItem(t *testing.T) {
48 t.Run("ExplicitLoopVar", func(t *testing.T) {
51 for i := 0; i < 4; i++ {
52 seen = append(seen, i)
56 // Will i++ still run?
60 // We can mutate i and it's observed by the loop. No special treatment of the loop var.
61 qt.Assert(t, seen, qt.DeepEquals, []int{0, 1, 2, 2, 3})
63 t.Run("Range", func(t *testing.T) {
66 for i := range iter.N(4) {
67 seen = append(seen, i)
70 // Can we actually modify the next value of i produced by the range?
75 // Range ignores any mutation to i.
76 qt.Assert(t, seen, qt.DeepEquals, []int{0, 1, 2, 3})