File: draw_test.go

package info (click to toggle)
golang-gonum-v1-plot 0.7.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,980 kB
  • sloc: sh: 81; makefile: 13
file content (117 lines) | stat: -rw-r--r-- 2,565 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package draw

import (
	"image/color"
	"reflect"
	"testing"

	"gonum.org/v1/plot/vg"
	"gonum.org/v1/plot/vg/recorder"
)

func TestCrop(t *testing.T) {
	ls := LineStyle{
		Color: color.NRGBA{0, 20, 0, 123},
		Width: 0.1 * vg.Inch,
	}
	var r1 recorder.Canvas
	c1 := NewCanvas(&r1, 6, 3)
	c11 := Crop(c1, 0, -3, 0, 0)
	c12 := Crop(c1, 3, 0, 0, 0)

	var r2 recorder.Canvas
	c2 := NewCanvas(&r2, 6, 3)
	c21 := Canvas{
		Canvas: c2.Canvas,
		Rectangle: vg.Rectangle{
			Min: vg.Point{X: 0, Y: 0},
			Max: vg.Point{X: 3, Y: 3},
		},
	}
	c22 := Canvas{
		Canvas: c2.Canvas,
		Rectangle: vg.Rectangle{
			Min: vg.Point{X: 3, Y: 0},
			Max: vg.Point{X: 6, Y: 3},
		},
	}
	str := "unexpected result: %+v != %+v"
	if c11.Rectangle != c21.Rectangle {
		t.Errorf(str, c11.Rectangle, c21.Rectangle)
	}
	if c12.Rectangle != c22.Rectangle {
		t.Errorf(str, c11.Rectangle, c21.Rectangle)
	}

	c11.StrokeLine2(ls, c11.Min.X, c11.Min.Y,
		c11.Min.X+3*vg.Inch, c11.Min.Y+3*vg.Inch)
	c12.StrokeLine2(ls, c12.Min.X, c12.Min.Y,
		c12.Min.X+3*vg.Inch, c12.Min.Y+3*vg.Inch)
	c21.StrokeLine2(ls, c21.Min.X, c21.Min.Y,
		c21.Min.X+3*vg.Inch, c21.Min.Y+3*vg.Inch)
	c22.StrokeLine2(ls, c22.Min.X, c22.Min.Y,
		c22.Min.X+3*vg.Inch, c22.Min.Y+3*vg.Inch)

	if !reflect.DeepEqual(r1.Actions, r2.Actions) {
		t.Errorf(str, r1.Actions, r2.Actions)
	}
}

func TestTile(t *testing.T) {
	var r recorder.Canvas
	c := NewCanvas(&r, 13, 7)
	const (
		rows = 2
		cols = 3
		pad  = 1
	)
	tiles := Tiles{
		Rows: rows, Cols: cols,
		PadTop: pad, PadBottom: pad,
		PadRight: pad, PadLeft: pad,
		PadX: pad, PadY: pad,
	}
	rectangles := [][]vg.Rectangle{
		{
			vg.Rectangle{
				Min: vg.Point{X: 1, Y: 4},
				Max: vg.Point{X: 4, Y: 6},
			},
			vg.Rectangle{
				Min: vg.Point{X: 5, Y: 4},
				Max: vg.Point{X: 8, Y: 6},
			},
			vg.Rectangle{
				Min: vg.Point{X: 9, Y: 4},
				Max: vg.Point{X: 12, Y: 6},
			},
		},
		{
			vg.Rectangle{
				Min: vg.Point{X: 1, Y: 1},
				Max: vg.Point{X: 4, Y: 3},
			},
			vg.Rectangle{
				Min: vg.Point{X: 5, Y: 1},
				Max: vg.Point{X: 8, Y: 3},
			},
			vg.Rectangle{
				Min: vg.Point{X: 9, Y: 1},
				Max: vg.Point{X: 12, Y: 3},
			},
		},
	}
	for j := 0; j < rows; j++ {
		for i := 0; i < cols; i++ {
			str := "row %d col %d unexpected result: %+v != %+v"
			tile := tiles.At(c, i, j)
			if tile.Rectangle != rectangles[j][i] {
				t.Errorf(str, j, i, tile.Rectangle, rectangles[j][i])
			}
		}
	}
}