cmpregress_test.go raw

   1  package utxostore
   2  
   3  import (
   4  	"testing"
   5  
   6  	"git.smesh.lol/btcd-lite/internal/utxo"
   7  	"git.smesh.lol/btcd-lite/internal/wire"
   8  )
   9  
  10  // TestComparisonIsOneOrder: every pair of outpoints in the base and the
  11  // creations of the node fixture orders the same way under the merge's
  12  // comparison and the set's.
  13  //
  14  // The merge's comparison used to or a hash comparison with an index
  15  // comparison, and -1 or -1 is -1, so two outpoints differing in both could
  16  // order one way there and the other way in the set. 26 of 225 pairs disagreed
  17  // over the node's own base; 0 do now.
  18  func TestComparisonIsOneOrder(t *testing.T) {
  19  	base := nodeEntriesAt4(t)
  20  	ts, _ := nodeSecondRange(t, base)
  21  	var ops []wire.OutPoint
  22  	for _, e := range base {
  23  		ops = append(ops, e.OutPoint)
  24  	}
  25  	for _, tc := range ts {
  26  		ops = append(ops, tc.OutPoint)
  27  	}
  28  	sign := func(x int) int {
  29  		switch {
  30  		case x < 0:
  31  			return -1
  32  		case x > 0:
  33  			return 1
  34  		}
  35  		return 0
  36  	}
  37  	for i := range ops {
  38  		for j := range ops {
  39  			byMerge := sign(cmpOp(ops[i], ops[j]))
  40  			bySet := sign(utxo.CompareOutPoints(ops[i], ops[j]))
  41  			byLess := 0
  42  			if utxo.LessEntry(utxo.Entry{OutPoint: ops[i]}, utxo.Entry{OutPoint: ops[j]}) {
  43  				byLess = -1
  44  			} else if utxo.LessEntry(utxo.Entry{OutPoint: ops[j]}, utxo.Entry{OutPoint: ops[i]}) {
  45  				byLess = 1
  46  			}
  47  			if byMerge != bySet || bySet != byLess {
  48  				t.Fatalf("%x:%d vs %x:%d: merge %d, set %d, less %d",
  49  					ops[i].Hash[:2], ops[i].Index, ops[j].Hash[:2], ops[j].Index, byMerge, bySet, byLess)
  50  			}
  51  		}
  52  	}
  53  	t.Logf("%d outpoints, %d pairs, one order", len(ops), len(ops)*len(ops))
  54  }
  55