github.com/m3db/m3@v1.5.0/src/aggregator/aggregation/quantile/cm/heap_test.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     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 cm
    22  
    23  import (
    24  	"math/rand"
    25  	"sort"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestMinHeapPushInDecreasingOrder(t *testing.T) {
    32  	h := &minHeap{}
    33  	iter := 10
    34  	for i := iter - 1; i >= 0; i-- {
    35  		h.Push(float64(i))
    36  		require.Equal(t, iter-i, h.Len())
    37  	}
    38  	validateSort(t, *h)
    39  	for i := 0; i < iter; i++ {
    40  		require.Equal(t, float64(i), h.Min())
    41  		require.Equal(t, float64(i), h.Pop())
    42  		validateInvariant(t, *h, 0)
    43  	}
    44  }
    45  
    46  func TestMinHeapPushInIncreasingOrder(t *testing.T) {
    47  	h := &minHeap{}
    48  	iter := 10
    49  	for i := 0; i < iter; i++ {
    50  		h.Push(float64(i))
    51  		require.Equal(t, i+1, h.Len())
    52  	}
    53  	validateSort(t, *h)
    54  	for i := 0; i < iter; i++ {
    55  		require.Equal(t, float64(i), h.Min())
    56  		require.Equal(t, float64(i), h.Pop())
    57  		validateInvariant(t, *h, 0)
    58  	}
    59  }
    60  
    61  func TestMinHeapPushInRandomOrderAndSort(t *testing.T) {
    62  	h := &minHeap{}
    63  	iter := 42
    64  	for i := 0; i < iter; i++ {
    65  		h.Push(rand.ExpFloat64())
    66  	}
    67  	validateSort(t, *h)
    68  }
    69  
    70  func validateSort(t *testing.T, h minHeap) {
    71  	t.Helper()
    72  	// copy heap before applying reference sort and minheap-sort
    73  	a := make([]float64, h.Len())
    74  	b := make([]float64, h.Len())
    75  	for i := 0; i < len(h); i++ {
    76  		a[i], b[i] = h[i], h[i]
    77  	}
    78  	sort.Sort(sort.Reverse(sort.Float64Slice(a)))
    79  	heap := (*minHeap)(&b)
    80  	heap.SortDesc()
    81  	require.Equal(t, a, b)
    82  }
    83  
    84  func validateInvariant(t *testing.T, h minHeap, i int) {
    85  	var (
    86  		n     = h.Len()
    87  		left  = 2*i + 1
    88  		right = 2*i + 2
    89  	)
    90  
    91  	if left < n {
    92  		require.True(t, h[i] <= h[left])
    93  		validateInvariant(t, h, left)
    94  	}
    95  
    96  	if right < n {
    97  		require.True(t, h[i] <= h[right])
    98  		validateInvariant(t, h, right)
    99  	}
   100  }