File: xy_test.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20230522.2e198f4-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 6,404 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 26
file content (79 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download | duplicates (4)
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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build example
// +build example

//
// Use "go test -tags=example" to run this test.

package main

import "testing"

func TestIJXYCorners(t *testing.T) {
	var d Dims
	d.Init(19, 100)
	xSize := (d.dim - 1) * d.squareWidth
	ySize := (d.dim - 1) * d.squareHeight
	ij := IJ{1, 1}
	xy := ij.XY(&d)
	if xy.x != d.xInset || xy.y != d.yInset+ySize {
		t.Errorf("%d got %d", ij, xy)
	}
	ij = IJ{1, d.dim}
	xy = ij.XY(&d)
	if xy.x != d.xInset || xy.y != d.yInset {
		t.Errorf("%d got %d", ij, xy)
	}
	ij = IJ{d.dim, 1}
	xy = ij.XY(&d)
	if xy.x != d.xInset+xSize || xy.y != d.yInset+ySize {
		t.Errorf("%d got %d", ij, xy)
	}
	ij = IJ{d.dim, d.dim}
	xy = ij.XY(&d)
	if xy.x != d.xInset+xSize || xy.y != d.yInset {
		t.Errorf("%d got %d", ij, xy)
	}
}

func TestIJXYCenterRoundTrip(t *testing.T) {
	var d Dims
	d.Init(19, 100)
	for i := 1; i <= d.dim; i++ {
		for j := 1; j <= d.dim; j++ {
			ij := IJ{i, j}
			xy := ij.XYCenter(&d)
			ij2, ok := xy.IJ(&d)
			if !ok {
				t.Error("failed to round trip")
			}
			if ij2 != ij {
				t.Errorf("%d round trip got %d\n", ij, ij2)
			}
		}
	}
}

func TestIJString(t *testing.T) {
	var d Dims
	d.Init(19, 100)
	ij := IJ{1, 1}
	if ij.String() != "A1" {
		t.Errorf("%#v prints as %q", ij, ij.String())
	}
	ij = IJ{1, 19}
	if ij.String() != "A19" {
		t.Errorf("%#v prints as %q", ij, ij.String())
	}
	ij = IJ{19, 1}
	if ij.String() != "S1" {
		t.Errorf("%#v prints as %q", ij, ij.String())
	}
	ij = IJ{19, 19}
	if ij.String() != "S19" {
		t.Errorf("%#v prints as %q", ij, ij.String())
	}
}