github.com/decred/dcrlnd@v0.7.6/tlv/record_test.go (about) 1 package tlv 2 3 import ( 4 "bytes" 5 "reflect" 6 "testing" 7 8 "github.com/davecgh/go-spew/spew" 9 ) 10 11 // TestSortRecords tests that SortRecords is able to properly sort records in 12 // place. 13 func TestSortRecords(t *testing.T) { 14 t.Parallel() 15 16 testCases := []struct { 17 preSort []Record 18 postSort []Record 19 }{ 20 // An empty slice requires no sorting. 21 { 22 preSort: []Record{}, 23 postSort: []Record{}, 24 }, 25 26 // An already sorted slice should be passed through. 27 { 28 preSort: []Record{ 29 MakeStaticRecord(1, nil, 0, nil, nil), 30 MakeStaticRecord(2, nil, 0, nil, nil), 31 MakeStaticRecord(3, nil, 0, nil, nil), 32 }, 33 postSort: []Record{ 34 MakeStaticRecord(1, nil, 0, nil, nil), 35 MakeStaticRecord(2, nil, 0, nil, nil), 36 MakeStaticRecord(3, nil, 0, nil, nil), 37 }, 38 }, 39 40 // We should be able to sort a randomized set of records . 41 { 42 preSort: []Record{ 43 MakeStaticRecord(9, nil, 0, nil, nil), 44 MakeStaticRecord(43, nil, 0, nil, nil), 45 MakeStaticRecord(1, nil, 0, nil, nil), 46 MakeStaticRecord(0, nil, 0, nil, nil), 47 }, 48 postSort: []Record{ 49 MakeStaticRecord(0, nil, 0, nil, nil), 50 MakeStaticRecord(1, nil, 0, nil, nil), 51 MakeStaticRecord(9, nil, 0, nil, nil), 52 MakeStaticRecord(43, nil, 0, nil, nil), 53 }, 54 }, 55 } 56 57 for i, testCase := range testCases { 58 SortRecords(testCase.preSort) 59 60 if !reflect.DeepEqual(testCase.preSort, testCase.postSort) { 61 t.Fatalf("#%v: wrong order: expected %v, got %v", i, 62 spew.Sdump(testCase.preSort), 63 spew.Sdump(testCase.postSort)) 64 } 65 } 66 } 67 68 // TestRecordMapTransformation tests that we're able to properly morph a set of 69 // records into a map using TlvRecordsToMap, then the other way around using 70 // the MapToTlvRecords method. 71 func TestRecordMapTransformation(t *testing.T) { 72 t.Parallel() 73 74 tlvBytes := []byte{1, 2, 3, 4} 75 encoder := StubEncoder(tlvBytes) 76 77 testCases := []struct { 78 records []Record 79 80 tlvMap map[uint64][]byte 81 }{ 82 // An empty set of records should yield an empty map, and the other 83 // way around. 84 { 85 records: []Record{}, 86 tlvMap: map[uint64][]byte{}, 87 }, 88 89 // We should be able to transform this set of records, then obtain 90 // the records back in the same order. 91 { 92 records: []Record{ 93 MakeStaticRecord(1, nil, 4, encoder, nil), 94 MakeStaticRecord(2, nil, 4, encoder, nil), 95 MakeStaticRecord(3, nil, 4, encoder, nil), 96 }, 97 tlvMap: map[uint64][]byte{ 98 1: tlvBytes, 99 2: tlvBytes, 100 3: tlvBytes, 101 }, 102 }, 103 } 104 105 for i, testCase := range testCases { 106 mappedRecords, err := RecordsToMap(testCase.records) 107 if err != nil { 108 t.Fatalf("#%v: unable to map records: %v", i, err) 109 } 110 111 if !reflect.DeepEqual(mappedRecords, testCase.tlvMap) { 112 t.Fatalf("#%v: incorrect record map: expected %v, got %v", 113 i, spew.Sdump(testCase.tlvMap), 114 spew.Sdump(mappedRecords)) 115 } 116 117 unmappedRecords := MapToRecords(mappedRecords) 118 119 for i := 0; i < len(testCase.records); i++ { 120 if unmappedRecords[i].Type() != testCase.records[i].Type() { 121 t.Fatalf("#%v: wrong type: expected %v, got %v", 122 i, unmappedRecords[i].Type(), 123 testCase.records[i].Type()) 124 } 125 126 var b bytes.Buffer 127 if err := unmappedRecords[i].Encode(&b); err != nil { 128 t.Fatalf("#%v: unable to encode record: %v", 129 i, err) 130 } 131 132 if !bytes.Equal(b.Bytes(), tlvBytes) { 133 t.Fatalf("#%v: wrong raw record: "+ 134 "expected %x, got %x", 135 i, tlvBytes, b.Bytes()) 136 } 137 138 if unmappedRecords[i].Size() != testCase.records[0].Size() { 139 t.Fatalf("#%v: wrong size: expected %v, "+ 140 "got %v", i, 141 unmappedRecords[i].Size(), 142 testCase.records[i].Size()) 143 } 144 } 145 } 146 }