From 900f36f1a937aa852194594088534f8e2ad8396f Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 18 Sep 2021 13:50:55 +1000 Subject: [PATCH] Add some tests verifying request map ordering --- requesting_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 requesting_test.go diff --git a/requesting_test.go b/requesting_test.go new file mode 100644 index 00000000..8599086e --- /dev/null +++ b/requesting_test.go @@ -0,0 +1,43 @@ +package torrent + +import ( + "testing" + + pp "github.com/anacrolix/torrent/peer_protocol" + qt "github.com/frankban/quicktest" +) + +func keysAsSlice(m map[Request]struct{}) (sl []Request) { + for k := range m { + sl = append(sl, k) + } + return +} + +func makeTypicalRequests() map[Request]struct{} { + m := make(map[Request]struct{}) + for p := pp.Integer(0); p < 4; p++ { + for c := pp.Integer(0); c < 16; c++ { + m[Request{p, ChunkSpec{c * defaultChunkSize, defaultChunkSize}}] = struct{}{} + } + } + return m +} + +func TestLogExampleRequestMapOrdering(t *testing.T) { + for k := range makeTypicalRequests() { + t.Log(k) + } + +} + +func TestRequestMapOrderingPersistent(t *testing.T) { + m := makeTypicalRequests() + // Shows that map order is persistent across separate range statements. + qt.Assert(t, keysAsSlice(m), qt.ContentEquals, keysAsSlice(m)) +} + +func TestRequestMapOrderAcrossInstances(t *testing.T) { + // This shows that different map instances with the same contents can have the same range order. + qt.Assert(t, keysAsSlice(makeTypicalRequests()), qt.ContentEquals, keysAsSlice(makeTypicalRequests())) +} -- 2.44.0