| 1 | // Copyright © 2018-2021 Wei Shen <[email protected]> |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | package unik |
| 22 | |
| 23 | import ( |
| 24 | "math/rand" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | func TestStreamVByte64(t *testing.T) { |
| 29 | ntests := 10000 |
| 30 | tests := make([][2]uint64, ntests) |
| 31 | var i int |
| 32 | for ; i < ntests/4; i++ { |
| 33 | tests[i] = [2]uint64{rand.Uint64(), rand.Uint64()} |
| 34 | } |
| 35 | for ; i < ntests/2; i++ { |
| 36 | tests[i] = [2]uint64{uint64(rand.Uint32()), uint64(rand.Uint32())} |
| 37 | } |
| 38 | for ; i < ntests*3/4; i++ { |
| 39 | tests[i] = [2]uint64{uint64(rand.Intn(65536)), uint64(rand.Intn(256))} |
| 40 | } |
| 41 | for ; i < ntests; i++ { |
| 42 | tests[i] = [2]uint64{uint64(rand.Intn(256)), uint64(rand.Intn(256))} |
| 43 | } |
| 44 | |
| 45 | for i, test := range tests { |
| 46 | buf := make([]byte, 16) |
| 47 | ctrl, n := PutUint64s(buf, test[0], test[1]) |
| 48 | |
| 49 | result, n2 := Uint64s(ctrl, buf[0:n]) |
| 50 | if n2 == 0 { |
| 51 | t.Errorf("#%d, wrong decoded number", i) |
| 52 | } |
| 53 | |
| 54 | if result[0] != test[0] || result[1] != test[1] { |
| 55 | t.Errorf("#%d, wrong decoded result: %d, %d, answer: %d, %d", i, result[0], result[1], test[0], test[1]) |
| 56 | } |
| 57 | // fmt.Printf("%d, %d => n=%d, buf=%v\n", test[0], test[1], n, buf[0:n]) |
| 58 | } |
| 59 | } |