Refine search
public void testCompare() { for (float x : VALUES) { for (float y : VALUES) { // note: spec requires only that the sign is the same assertEquals(x + ", " + y, Float.valueOf(x).compareTo(y), Floats.compare(x, y)); } } }
@Override public int hashCode() { int result = 1; for (int i = start; i < end; i++) { result = 31 * result + Floats.hashCode(array[i]); } return result; }
public void testAsList_subList_toArray_roundTrip() { float[] array = {(float) 0, (float) 1, (float) 2, (float) 3}; List<Float> list = Floats.asList(array); assertTrue( Arrays.equals(new float[] {(float) 1, (float) 2}, Floats.toArray(list.subList(1, 3)))); assertTrue(Arrays.equals(new float[] {}, Floats.toArray(list.subList(2, 2)))); }
@Test public void testNotEqualsByFloat() throws Exception { String query = "SELECT a_float -- and here comment\n" + "FROM aTable WHERE organization_id=? and a_float != 0.01d and a_float <= 0.02d"; Properties props = new Properties(TEST_PROPERTIES); props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 2)); // Execute at timestamp 2 Connection conn = DriverManager.getConnection(PHOENIX_JDBC_URL, props); try { PreparedStatement statement = conn.prepareStatement(query); statement.setString(1, tenantId); ResultSet rs = statement.executeQuery(); assertTrue (rs.next()); assertTrue(Floats.compare(rs.getFloat(1), 0.02f) == 0); assertFalse(rs.next()); } finally { conn.close(); } }
@Test public void testRoundFloat() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); ResultSet rs = conn.createStatement().executeQuery( "SELECT ROUND(fl), ROUND(fl, 1), ROUND(fl, 2), ROUND(fl, 3) FROM " + tableName); assertTrue(rs.next()); assertEquals(0, Floats.compare(1, rs.getFloat(1))); assertEquals(0, Floats.compare(1.3f, rs.getFloat(2))); assertEquals(0, Floats.compare(1.26f, rs.getFloat(3))); assertEquals(0, Floats.compare(1.264f, rs.getFloat(4))); }
/** * Returns the index of the last appearance of the value {@code target} in {@code array}. Note * that this always returns {@code -1} when {@code target} is {@code NaN}. * * @param array an array of {@code float} values, possibly empty * @param target a primitive {@code float} value * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int lastIndexOf(float[] array, float target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. Note * that this always returns {@code -1} when {@code target} is {@code NaN}. * * @param array an array of {@code float} values, possibly empty * @param target a primitive {@code float} value * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int indexOf(float[] array, float target) { return indexOf(array, target, 0, array.length); }
@Override public PersistedData serializeCollection(Collection<Float> value, SerializationContext context) { return context.create(Floats.toArray(value)); }
private void initFloatPredicate() { if (floatPredicate != null) { return; } synchronized (initLock) { if (floatPredicate != null) { return; } if (value == null) { floatPredicate = DruidFloatPredicate.MATCH_NULL_ONLY; return; } final Float valueAsFloat = Floats.tryParse(value); if (valueAsFloat == null) { floatPredicate = DruidFloatPredicate.ALWAYS_FALSE; } else { final int floatBits = Float.floatToIntBits(valueAsFloat); floatPredicate = input -> Float.floatToIntBits(input) == floatBits; } } }
private static List<Float> asList(Float[] values) { float[] temp = new float[values.length]; for (int i = 0; i < values.length; i++) { temp[i] = checkNotNull(values[i]); // checkNotNull for GWT (do not optimize). } return Floats.asList(temp); }
/** * Returns an array containing the same values as {@code array}, but * guaranteed to be of a specified minimum length. If {@code array} already * has a length of at least {@code minLength}, it is returned directly. * Otherwise, a new array of size {@code minLength + padding} is returned, * containing the values of {@code array}, and zeroes in the remaining places. * * @param array the source array * @param minLength the minimum length the returned array must guarantee * @param padding an extra amount to "grow" the array by if growth is * necessary * @throws IllegalArgumentException if {@code minLength} or {@code padding} is * negative * @return an array containing the values of {@code array}, with guaranteed * minimum length {@code minLength} */ public static float[] ensureCapacity(float[] array, int minLength, int padding) { checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); checkArgument(padding >= 0, "Invalid padding: %s", padding); return (array.length < minLength) ? copyOf(array, minLength + padding) : array; }
@Test public void testNotEqualsByUnsignedFloat() throws Exception { String query = "SELECT a_unsigned_float -- and here comment\n" + "FROM aTable WHERE organization_id=? and a_unsigned_float != 0.01d and a_unsigned_float <= 0.02d"; Properties props = new Properties(TEST_PROPERTIES); props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 2)); // Execute at timestamp 2 Connection conn = DriverManager.getConnection(PHOENIX_JDBC_URL, props); try { PreparedStatement statement = conn.prepareStatement(query); statement.setString(1, tenantId); ResultSet rs = statement.executeQuery(); assertTrue (rs.next()); assertTrue(Floats.compare(rs.getFloat(1), 0.02f) == 0); assertFalse(rs.next()); } finally { conn.close(); } }
@Test public void testRoundUnsignedFloat() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); ResultSet rs = conn.createStatement().executeQuery( "SELECT ROUND(unfl), ROUND(unfl, 1), ROUND(unfl, 2), ROUND(unfl, 3) FROM " + tableName); assertTrue(rs.next()); assertEquals(0, Floats.compare(1, rs.getFloat(1))); assertEquals(0, Floats.compare(1.3f, rs.getFloat(2))); assertEquals(0, Floats.compare(1.26f, rs.getFloat(3))); assertEquals(0, Floats.compare(1.264f, rs.getFloat(4))); }
/** * Returns the index of the last appearance of the value {@code target} in {@code array}. Note * that this always returns {@code -1} when {@code target} is {@code NaN}. * * @param array an array of {@code float} values, possibly empty * @param target a primitive {@code float} value * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int lastIndexOf(float[] array, float target) { return lastIndexOf(array, target, 0, array.length); }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. Note * that this always returns {@code -1} when {@code target} is {@code NaN}. * * @param array an array of {@code float} values, possibly empty * @param target a primitive {@code float} value * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int indexOf(float[] array, float target) { return indexOf(array, target, 0, array.length); }
public void testToArray_withNull() { List<Float> list = Arrays.asList((float) 0, (float) 1, null); try { Floats.toArray(list); fail(); } catch (NullPointerException expected) { } }
@Nullable public static Float convertObjectToFloat(@Nullable Object valObj, boolean reportParseExceptions) { if (valObj == null) { return null; } if (valObj instanceof Float) { return (Float) valObj; } else if (valObj instanceof Number) { return ((Number) valObj).floatValue(); } else if (valObj instanceof String) { Float ret = Floats.tryParse((String) valObj); if (reportParseExceptions && ret == null) { throw new ParseException("could not convert value [%s] to float", valObj); } return ret; } else { throw new ParseException("Unknown type[%s]", valObj.getClass()); } }
/** * Fails if the values in the subject were expected to be within the tolerance of the given * values but were not <i>or</i> if they were expected <i>not</i> to be within the tolerance but * were. The subject and tolerance are specified earlier in the fluent call chain. */ public void of(float... expected) { ofElementsIn(Floats.asList(expected)); }
/** * Returns an array containing the same values as {@code array}, but * guaranteed to be of a specified minimum length. If {@code array} already * has a length of at least {@code minLength}, it is returned directly. * Otherwise, a new array of size {@code minLength + padding} is returned, * containing the values of {@code array}, and zeroes in the remaining places. * * @param array the source array * @param minLength the minimum length the returned array must guarantee * @param padding an extra amount to "grow" the array by if growth is * necessary * @throws IllegalArgumentException if {@code minLength} or {@code padding} is * negative * @return an array containing the values of {@code array}, with guaranteed * minimum length {@code minLength} */ public static float[] ensureCapacity( float[] array, int minLength, int padding) { checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); checkArgument(padding >= 0, "Invalid padding: %s", padding); return (array.length < minLength) ? copyOf(array, minLength + padding) : array; }
@Test public void testSumFloat() throws Exception { initSumDoubleValues(null); String query = "SELECT SUM(f) FROM SumDoubleTest"; Properties props = new Properties(TEST_PROPERTIES); Connection conn = DriverManager.getConnection(PHOENIX_JDBC_URL, props); try { PreparedStatement statement = conn.prepareStatement(query); ResultSet rs = statement.executeQuery(); assertTrue (rs.next()); assertTrue(Floats.compare(rs.getFloat(1), 0.15f)==0); assertFalse(rs.next()); } finally { conn.close(); } }