File: uniq_test.go

package info (click to toggle)
golang-github-shenwei356-util 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: makefile: 2
file content (74 lines) | stat: -rw-r--r-- 1,344 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package numbers

import (
	"math/rand"
	"testing"
)

var data []*[]uint64
var data2 []*[]uint64

func init() {
	N := 100000 // N lists
	n := 300    // n elements for a list
	data = make([]*[]uint64, N)
	data2 = make([]*[]uint64, N)
	for i := 0; i < N; i++ {
		_n := rand.Intn(n)
		if _n < 0 {
			_n = -_n
		}
		_data := make([]uint64, _n)
		for j := 0; j < _n; j++ {
			_data[j] = uint64(float64(rand.Intn(_n)) / float64(1))
		}
		_data2 := make([]uint64, _n)
		copy(_data2, _data)

		data[i] = &_data
		data2[i] = &_data2
	}
}

func TestUniq(t *testing.T) {
	for _, _data := range data {
		u1 := Uniq(_data)

		c := make([]uint64, len(*_data))
		copy(c, *_data)

		UniqInplace(&c)

		// fmt.Printf("original: %v\n", _data)
		// fmt.Printf(" Inplace: %v\n", c)
		// fmt.Printf("    uniq: %v\n", *u1)
		if !Equal(*u1, c) {
			// fmt.Printf("original: %v\n", _data)
			// fmt.Printf(" Inplace: %v\n", c)
			// fmt.Printf("    uniq: %v\n", *u1)
			t.Error("error")
		}
	}
}

var result *[]uint64

func BenchmarkUniq(b *testing.B) {
	var _result *[]uint64
	// for i := 0; i < b.N; i++ {
	for _, _data := range data {
		_result = Uniq(_data)
	}
	// }
	result = _result
}

func BenchmarkUniqInplace(b *testing.B) {
	var _result *[]uint64
	// for i := 0; i < b.N; i++ {
	for _, _data := range data2 {
		UniqInplace(_data)
	}
	// }
	result = _result
}