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