summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/bukkit/LocationTest.java
blob: fa24776335b83fa3aa56e3966b7038d56c8682f1 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package org.bukkit;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.List;
import java.util.Random;

import org.bukkit.util.Vector;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import com.google.common.collect.ImmutableList;

@RunWith(Parameterized.class)
public class LocationTest {
    private static final double δ = 1.0 / 1000000;
    /**
     * <pre>
     * a² + b² = c², a = b
     * => 2∙(a²) = 2∙(b²) = c², c = 1
     * => 2∙(a²) = 1
     * => a² = 1/2
     * => a = √(1/2) ∎
     * </pre>
     */
    private static final double HALF_UNIT = Math.sqrt(1 / 2f);
    /**
     * <pre>
     * a² + b² = c², c = √(1/2)
     * => a² + b² = √(1/2)², a = b
     * => 2∙(a²) = 2∙(b²) = 1/2
     * => a² = 1/4
     * => a = √(1/4) ∎
     * </pre>
     */
    private static final double HALF_HALF_UNIT = Math.sqrt(1 / 4f);

    @Parameters(name= "{index}: {0}")
    public static List<Object[]> data() {
        Random RANDOM = new Random(1l); // Test is deterministic
        int r = 0;
        return ImmutableList.<Object[]>of(
            new Object[] { "X",
                1, 0, 0,
                270, 0
            },
            new Object[] { "-X",
                -1, 0, 0,
                90, 0
            },
            new Object[] { "Z",
                0, 0, 1,
                0, 0
            },
            new Object[] { "-Z",
                0, 0, -1,
                180, 0
            },
            new Object[] { "Y",
                0, 1, 0,
                0, -90 // Zero is here as a "default" value
            },
            new Object[] { "-Y",
                0, -1, 0,
                0, 90 // Zero is here as a "default" value
            },
            new Object[] { "X Z",
                HALF_UNIT, 0, HALF_UNIT,
                (270 + 360) / 2, 0
            },
            new Object[] { "X -Z",
                HALF_UNIT, 0, -HALF_UNIT,
                (270 + 180) / 2, 0
            },
            new Object[] { "-X -Z",
                -HALF_UNIT, 0, -HALF_UNIT,
                (90 + 180) / 2, 0
            },
            new Object[] { "-X Z",
                -HALF_UNIT, 0, HALF_UNIT,
                (90 + 0) / 2, 0
            },
            new Object[] { "X Y Z",
                HALF_HALF_UNIT, HALF_UNIT, HALF_HALF_UNIT,
                (270 + 360) / 2, -45
            },
            new Object[] { "-X -Y -Z",
                -HALF_HALF_UNIT, -HALF_UNIT, -HALF_HALF_UNIT,
                (90 + 180) / 2, 45
            },
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++),
            getRandom(RANDOM, r++)
        );
    }

    private static Object[] getRandom(Random random, int index) {
        final double YAW_FACTOR = 360;
        final double YAW_OFFSET = 0;
        final double PITCH_FACTOR = 180;
        final double PITCH_OFFSET = -90;
        final double CARTESIAN_FACTOR = 256;
        final double CARTESIAN_OFFSET = -128;

        Vector vector;
        Location location;
        if (random.nextBoolean()) {
            float pitch = (float) (random.nextDouble() * PITCH_FACTOR + PITCH_OFFSET);
            float yaw = (float) (random.nextDouble() * YAW_FACTOR + YAW_OFFSET);

            location = getEmptyLocation();
            location.setPitch(pitch);
            location.setYaw(yaw);

            vector = location.getDirection();
        } else {
            double x = random.nextDouble() * CARTESIAN_FACTOR + CARTESIAN_OFFSET;
            double y = random.nextDouble() * CARTESIAN_FACTOR + CARTESIAN_OFFSET;
            double z = random.nextDouble() * CARTESIAN_FACTOR + CARTESIAN_OFFSET;

            location = getEmptyLocation();
            vector = new Vector(x, y, z).normalize();

            location.setDirection(vector);
        }

        return new Object[] { "R" + index,
            vector.getX(), vector.getY(), vector.getZ(),
            location.getYaw(), location.getPitch()
        };
    }

    @Parameter(0)
    public String nane;
    @Parameter(1)
    public double x;
    @Parameter(2)
    public double y;
    @Parameter(3)
    public double z;
    @Parameter(4)
    public float yaw;
    @Parameter(5)
    public float pitch;

    @Test
    public void testExpectedPitchYaw() {
        Location location = getEmptyLocation().setDirection(getVector());

        assertThat((double) location.getYaw(), is(closeTo(yaw, δ)));
        assertThat((double) location.getPitch(), is(closeTo(pitch, δ)));
    }

    @Test
    public void testExpectedXYZ() {
        Vector vector = getLocation().getDirection();

        assertThat(vector.getX(), is(closeTo(x, δ)));
        assertThat(vector.getY(), is(closeTo(y, δ)));
        assertThat(vector.getZ(), is(closeTo(z, δ)));
    }

    private Vector getVector() {
        return new Vector(x, y, z);
    }

    private static Location getEmptyLocation() {
        return new Location(null, 0, 0, 0);
    }

    private Location getLocation() {
        Location location = getEmptyLocation();
        location.setYaw(yaw);
        location.setPitch(pitch);
        return location;
    }
}