Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
LocationTest.java
1 package org.bukkit;
2 
3 import static org.hamcrest.Matchers.*;
4 import static org.junit.Assert.*;
5 
6 import java.util.List;
7 import java.util.Random;
8 
9 import org.bukkit.util.Vector;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.Parameterized;
13 import org.junit.runners.Parameterized.Parameter;
14 import org.junit.runners.Parameterized.Parameters;
15 
16 import com.google.common.collect.ImmutableList;
17 
18 @RunWith(Parameterized.class)
19 public class LocationTest {
20  private static final double δ = 1.0 / 1000000;
21  /**
22  * <pre>
23  * a² + b² = c², a = b
24  * => 2∙(a²) = 2∙(b²) = c², c = 1
25  * => 2∙(a²) = 1
26  * => a² = 1/2
27  * => a = √(1/2) ∎
28  * </pre>
29  */
30  private static final double HALF_UNIT = Math.sqrt(1 / 2f);
31  /**
32  * <pre>
33  * a² + b² = c², c = √(1/2)
34  * => a² + b² = √(1/2)², a = b
35  * => 2∙(a²) = 2∙(b²) = 1/2
36  * => a² = 1/4
37  * => a = √(1/4) ∎
38  * </pre>
39  */
40  private static final double HALF_HALF_UNIT = Math.sqrt(1 / 4f);
41 
42  @Parameters(name= "{index}: {0}")
43  public static List<Object[]> data() {
44  Random RANDOM = new Random(1l); // Test is deterministic
45  int r = 0;
46  return ImmutableList.<Object[]>of(
47  new Object[] { "X",
48  1, 0, 0,
49  270, 0
50  },
51  new Object[] { "-X",
52  -1, 0, 0,
53  90, 0
54  },
55  new Object[] { "Z",
56  0, 0, 1,
57  0, 0
58  },
59  new Object[] { "-Z",
60  0, 0, -1,
61  180, 0
62  },
63  new Object[] { "Y",
64  0, 1, 0,
65  0, -90 // Zero is here as a "default" value
66  },
67  new Object[] { "-Y",
68  0, -1, 0,
69  0, 90 // Zero is here as a "default" value
70  },
71  new Object[] { "X Z",
72  HALF_UNIT, 0, HALF_UNIT,
73  (270 + 360) / 2, 0
74  },
75  new Object[] { "X -Z",
76  HALF_UNIT, 0, -HALF_UNIT,
77  (270 + 180) / 2, 0
78  },
79  new Object[] { "-X -Z",
80  -HALF_UNIT, 0, -HALF_UNIT,
81  (90 + 180) / 2, 0
82  },
83  new Object[] { "-X Z",
84  -HALF_UNIT, 0, HALF_UNIT,
85  (90 + 0) / 2, 0
86  },
87  new Object[] { "X Y Z",
88  HALF_HALF_UNIT, HALF_UNIT, HALF_HALF_UNIT,
89  (270 + 360) / 2, -45
90  },
91  new Object[] { "-X -Y -Z",
92  -HALF_HALF_UNIT, -HALF_UNIT, -HALF_HALF_UNIT,
93  (90 + 180) / 2, 45
94  },
95  getRandom(RANDOM, r++),
96  getRandom(RANDOM, r++),
97  getRandom(RANDOM, r++),
98  getRandom(RANDOM, r++),
99  getRandom(RANDOM, r++),
100  getRandom(RANDOM, r++),
101  getRandom(RANDOM, r++),
102  getRandom(RANDOM, r++),
103  getRandom(RANDOM, r++),
104  getRandom(RANDOM, r++),
105  getRandom(RANDOM, r++),
106  getRandom(RANDOM, r++),
107  getRandom(RANDOM, r++),
108  getRandom(RANDOM, r++),
109  getRandom(RANDOM, r++),
110  getRandom(RANDOM, r++),
111  getRandom(RANDOM, r++),
112  getRandom(RANDOM, r++)
113  );
114  }
115 
116  private static Object[] getRandom(Random random, int index) {
117  final double YAW_FACTOR = 360;
118  final double YAW_OFFSET = 0;
119  final double PITCH_FACTOR = 180;
120  final double PITCH_OFFSET = -90;
121  final double CARTESIAN_FACTOR = 256;
122  final double CARTESIAN_OFFSET = -128;
123 
124  Vector vector;
125  Location location;
126  if (random.nextBoolean()) {
127  float pitch = (float) (random.nextDouble() * PITCH_FACTOR + PITCH_OFFSET);
128  float yaw = (float) (random.nextDouble() * YAW_FACTOR + YAW_OFFSET);
129 
130  location = getEmptyLocation();
131  location.setPitch(pitch);
132  location.setYaw(yaw);
133 
134  vector = location.getDirection();
135  } else {
136  double x = random.nextDouble() * CARTESIAN_FACTOR + CARTESIAN_OFFSET;
137  double y = random.nextDouble() * CARTESIAN_FACTOR + CARTESIAN_OFFSET;
138  double z = random.nextDouble() * CARTESIAN_FACTOR + CARTESIAN_OFFSET;
139 
140  location = getEmptyLocation();
141  vector = new Vector(x, y, z).normalize();
142 
143  location.setDirection(vector);
144  }
145 
146  return new Object[] { "R" + index,
147  vector.getX(), vector.getY(), vector.getZ(),
148  location.getYaw(), location.getPitch()
149  };
150  }
151 
152  @Parameter(0)
153  public String nane;
154  @Parameter(1)
155  public double x;
156  @Parameter(2)
157  public double y;
158  @Parameter(3)
159  public double z;
160  @Parameter(4)
161  public float yaw;
162  @Parameter(5)
163  public float pitch;
164 
165  @Test
166  public void testExpectedPitchYaw() {
167  Location location = getEmptyLocation().setDirection(getVector());
168 
169  assertThat((double) location.getYaw(), is(closeTo(yaw, δ)));
170  assertThat((double) location.getPitch(), is(closeTo(pitch, δ)));
171  }
172 
173  @Test
174  public void testExpectedXYZ() {
175  Vector vector = getLocation().getDirection();
176 
177  assertThat(vector.getX(), is(closeTo(x, δ)));
178  assertThat(vector.getY(), is(closeTo(y, δ)));
179  assertThat(vector.getZ(), is(closeTo(z, δ)));
180  }
181 
182  private Vector getVector() {
183  return new Vector(x, y, z);
184  }
185 
186  private static Location getEmptyLocation() {
187  return new Location(null, 0, 0, 0);
188  }
189 
190  private Location getLocation() {
191  Location location = getEmptyLocation();
192  location.setYaw(yaw);
193  location.setPitch(pitch);
194  return location;
195  }
196 }
Location setDirection(Vector vector)
Definition: Location.java:264
void setPitch(float pitch)
Definition: Location.java:217
Vector getDirection()
Definition: Location.java:244
void setYaw(float yaw)
Definition: Location.java:183