github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/astrobwt/astrobwt_test.go (about)

     1  package astrobwt
     2  
     3  import "math/rand"
     4  import "testing"
     5  
     6  // see https://www.geeksforgeeks.org/burrows-wheeler-data-transform-algorithm/
     7  func TestBWTAndInverseTransform(t *testing.T) {
     8  
     9  	tests := []struct {
    10  		input string
    11  		bwt   string
    12  	}{
    13  		{"BANANA", "ANNB$AA"}, // from https://www.geeksforgeeks.org/burrows-wheeler-data-transform-algorithm/
    14  		{"abracadabra", "ard$rcaaaabb"},
    15  		{"appellee", "e$elplepa"},
    16  		{"GATGCGAGAGATG", "GGGGGGTCAA$TAA"},
    17  		{"abcdefg", "g$abcdef"},
    18  	}
    19  
    20  	for _, test := range tests {
    21  		trans2, eos := BWT([]byte(test.input))
    22  		trans2[eos] = '$'
    23  
    24  		if string(trans2) != test.bwt {
    25  			t.Errorf("Test failed: Transform %s", test.input)
    26  		}
    27  		if string(InverseTransform([]byte(trans2), '$')) != test.input {
    28  			t.Errorf("Test failed: InverseTransform  expected '%s' actual '%s`", test.input, string(InverseTransform([]byte(trans2), '$')))
    29  		}
    30  
    31  		p := POW([]byte(test.input))
    32  		p0 := POW_0alloc([]byte(test.input))
    33  
    34  		if string(p[:]) != string(p0[:]) {
    35  			t.Error("Test failed: difference between pow and pow_0alloc")
    36  
    37  		}
    38  	}
    39  
    40  }
    41  
    42  func TestFromSuffixArray(t *testing.T) {
    43  	s := "GATGCGAGAGATG"
    44  	trans := "GGGGGGTCAA$TAA"
    45  
    46  	sa := SuffixArray([]byte(s))
    47  	B, err := FromSuffixArray([]byte(s), sa, '$')
    48  	if err != nil {
    49  		t.Error("Test failed: FromSuffixArray error")
    50  	}
    51  	if string(B) != trans {
    52  		t.Error("Test failed: FromSuffixArray returns wrong result")
    53  	}
    54  }
    55  
    56  func TestPow_Powalloc(t *testing.T) {
    57  
    58  	p := POW([]byte{0, 0, 0, 0})
    59  	p0 := POW_0alloc([]byte{0, 0, 0, 0})
    60  	if string(p[:]) != string(p0[:]) {
    61  		t.Error("Test failed: POW and POW_0alloc returns different ")
    62  	}
    63  }
    64  
    65  var cases [][]byte
    66  
    67  func init() {
    68  	rand.Seed(1)
    69  	alphabet := "abcdefghjijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
    70  	n := len(alphabet)
    71  	_ = n
    72  	scales := []int{100000}
    73  	cases = make([][]byte, len(scales))
    74  	for i, scale := range scales {
    75  		l := scale
    76  		buf := make([]byte, int(l))
    77  		for j := 0; j < int(l); j++ {
    78  			buf[j] = byte(rand.Uint32() & 0xff) //alphabet[rand.Intn(n)]
    79  		}
    80  		cases[i] = buf
    81  	}
    82  	POW([]byte{0x99})
    83  }
    84  
    85  var result []byte
    86  
    87  func BenchmarkTransform(t *testing.B) {
    88  	var r []byte
    89  	var err error
    90  	for i := 0; i < t.N; i++ {
    91  		r, err = Transform(cases[0], '$')
    92  		if err != nil {
    93  			t.Error(err)
    94  			return
    95  		}
    96  	}
    97  	result = r
    98  }
    99  
   100  func BenchmarkTransform_quick(t *testing.B) {
   101  	var r []byte
   102  	for i := 0; i < t.N; i++ {
   103  		//r, err = Transform(cases[0], '$')
   104  		r, _ = BWT(cases[0])
   105  	}
   106  	result = r
   107  }
   108  
   109  func BenchmarkPOW(t *testing.B) {
   110  	for i := 0; i < t.N; i++ {
   111  		rand.Read(cases[0][:])
   112  		_ = POW(cases[0][:])
   113  	}
   114  }
   115  
   116  func BenchmarkPOW_0alloc(t *testing.B) {
   117  	for i := 0; i < t.N; i++ {
   118  		rand.Read(cases[0][:])
   119  		_ = POW_0alloc(cases[0][:])
   120  	}
   121  }