1
package org.apache.lucene.search.function;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.lucene.util.LuceneTestCase;
21
import org.junit.Test;
26
public class TestDocValues extends LuceneTestCase {
29
public void testGetMinValue() {
30
float[] innerArray = new float[] { 1.0f, 2.0f, -1.0f, 100.0f };
31
DocValuesTestImpl docValues = new DocValuesTestImpl(innerArray);
32
assertEquals("-1.0f is the min value in the source array", -1.0f, docValues
35
// test with without values - NaN
36
innerArray = new float[] {};
37
docValues = new DocValuesTestImpl(innerArray);
38
assertTrue("max is NaN - no values in inner array", Float.isNaN(docValues
42
public void testGetMaxValue() {
43
float[] innerArray = new float[] { 1.0f, 2.0f, -1.0f, 10.0f };
44
DocValuesTestImpl docValues = new DocValuesTestImpl(innerArray);
45
assertEquals("10.0f is the max value in the source array", 10.0f, docValues
48
innerArray = new float[] { -3.0f, -1.0f, -100.0f };
49
docValues = new DocValuesTestImpl(innerArray);
50
assertEquals("-1.0f is the max value in the source array", -1.0f, docValues
53
innerArray = new float[] { -3.0f, -1.0f, 100.0f, Float.MAX_VALUE,
54
Float.MAX_VALUE - 1 };
55
docValues = new DocValuesTestImpl(innerArray);
56
assertEquals(Float.MAX_VALUE + " is the max value in the source array",
57
Float.MAX_VALUE, docValues.getMaxValue(), 0);
59
// test with without values - NaN
60
innerArray = new float[] {};
61
docValues = new DocValuesTestImpl(innerArray);
62
assertTrue("max is NaN - no values in inner array", Float.isNaN(docValues
67
public void testGetAverageValue() {
68
float[] innerArray = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
69
DocValuesTestImpl docValues = new DocValuesTestImpl(innerArray);
70
assertEquals("the average is 1.0f", 1.0f, docValues.getAverageValue(), 0);
72
innerArray = new float[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
73
docValues = new DocValuesTestImpl(innerArray);
74
assertEquals("the average is 3.5f", 3.5f, docValues.getAverageValue(), 0);
76
// test with negative values
77
innerArray = new float[] { -1.0f, 2.0f };
78
docValues = new DocValuesTestImpl(innerArray);
79
assertEquals("the average is 0.5f", 0.5f, docValues.getAverageValue(), 0);
81
// test with without values - NaN
82
innerArray = new float[] {};
83
docValues = new DocValuesTestImpl(innerArray);
84
assertTrue("the average is NaN - no values in inner array", Float
85
.isNaN(docValues.getAverageValue()));
88
static class DocValuesTestImpl extends DocValues {
91
DocValuesTestImpl(float[] innerArray) {
92
this.innerArray = innerArray;
96
* @see org.apache.lucene.search.function.DocValues#floatVal(int)
99
public float floatVal(int doc) {
100
return innerArray[doc];
104
* @see org.apache.lucene.search.function.DocValues#toString(int)
107
public String toString(int doc) {
108
return Integer.toString(doc);