File: ghistogram_map_test.go

package info (click to toggle)
golang-github-couchbase-ghistogram 0.0.0%2Bgit20170308.21.d910dd0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 104 kB
  • sloc: makefile: 2
file content (68 lines) | stat: -rw-r--r-- 2,097 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
//  Copyright (c) 2017 Couchbase, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the
//  License. You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing,
//  software distributed under the License is distributed on an "AS
//  IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
//  express or implied. See the License for the specific language
//  governing permissions and limitations under the License.

// Package ghistogram provides a simple histogram of uint64's that
// avoids heap allocations (garbage creation) during data processing.

package ghistogram

import (
	"strings"
	"testing"
)

func initAndFetchHistograms(t *testing.T) (Histograms, string, string) {
	histograms := make(Histograms)
	histograms["test1"] = NewNamedHistogram("test1 (µs)", 10, 2, 2)
	histograms["test2"] = NewNamedHistogram("test2 (µs)", 10, 2, 2)

	histograms["test1"].Add(uint64(1), 2)
	histograms["test1"].Add(uint64(3), 4)
	histograms["test2"].Add(uint64(2), 1)
	histograms["test2"].Add(uint64(4), 3)

	test1 := `test1 (µs) (6 Total)
[0 - 2]   33.33%   33.33% ############### (2)
[2 - 4]   66.67%  100.00% ############################## (4)
`

	test2 := `test2 (µs) (4 Total)
[2 - 4]   25.00%   25.00% ########## (1)
[4 - 8]   75.00%  100.00% ############################## (3)
`

	return histograms, test1, test2
}

func TestStringHistograms(t *testing.T) {
	histograms, exp1, exp2 := initAndFetchHistograms(t)

	output := histograms.String()
	if !strings.Contains(output, exp1) || !strings.Contains(output, exp2) {
		t.Errorf("Unexpected content in String()")
	}
}

func TestAddAllHistograms(t *testing.T) {
	histograms, exp1, exp2 := initAndFetchHistograms(t)

	newhistograms := make(Histograms)
	newhistograms.AddAll(histograms)

	output := newhistograms.String()

	if !strings.Contains(output, exp1) || !strings.Contains(output, exp2) {
		t.Errorf("Unexpected content in String() after AddAll")
	}
}