<commit_before>// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package cpu

import (
	"math"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestGetSampleWeights(t *testing.T) {
	tests := []struct {
		name      string
		frequency time.Duration
		halflife  time.Duration
		oldWeight float64
	}{
		{
			name:      "simple equal values",
			frequency: 2 * time.Second,
			halflife:  2 * time.Second,
			oldWeight: .5,
		},
		{
			name:      "two periods values",
			frequency: 2 * time.Second,
			halflife:  4 * time.Second,
			oldWeight: math.Sqrt(.5),
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			assert := assert.New(t)

			newWeight, oldWeight := getSampleWeights(test.frequency, test.halflife)
			assert.Equal(1-test.oldWeight, newWeight)
			assert.Equal(test.oldWeight, oldWeight)
		})
	}
}<commit_msg>Fix flaky float equality test (#1475)<commit_after>// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package cpu

import (
	"math"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

const epsilon = 1e-9

func TestGetSampleWeights(t *testing.T) {
	tests := []struct {
		name      string
		frequency time.Duration
		halflife  time.Duration
		oldWeight float64
	}{
		{
			name:      "simple equal values",
			frequency: 2 * time.Second,
			halflife:  2 * time.Second,
			oldWeight: .5,
		},
		{
			name:      "two periods values",
			frequency: 2 * time.Second,
			halflife:  4 * time.Second,
			oldWeight: math.Sqrt(.5),
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			assert := assert.New(t)

			newWeight, oldWeight := getSampleWeights(test.frequency, test.halflife)
			assert.InDelta(1-test.oldWeight, newWeight, epsilon)
			assert.InDelta(test.oldWeight, oldWeight, epsilon)
		})
	}
}
{'fraction_non_alphanumeric': 0.13350785340314136, 'fraction_numerical': 0.020942408376963352, 'mean_word_length': 3.1543478260869566, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4085937', 'n_tokens_mistral': 790, 'n_tokens_neox': 687, 'n_words': 166}