pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / TestFieldCache.java
1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.lucene.analysis.MockAnalyzer;
20 import org.apache.lucene.document.Document;
21 import org.apache.lucene.document.Field;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.RandomIndexWriter;
24 import org.apache.lucene.store.Directory;
25 import org.apache.lucene.util.LuceneTestCase;
26 import java.io.IOException;
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
29
30 public class TestFieldCache extends LuceneTestCase {
31   protected IndexReader reader;
32   private int NUM_DOCS;
33   private Directory directory;
34
35   @Override
36   public void setUp() throws Exception {
37     super.setUp();
38     NUM_DOCS = atLeast(1000);
39     directory = newDirectory();
40     RandomIndexWriter writer= new RandomIndexWriter(random, directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
41     long theLong = Long.MAX_VALUE;
42     double theDouble = Double.MAX_VALUE;
43     byte theByte = Byte.MAX_VALUE;
44     short theShort = Short.MAX_VALUE;
45     int theInt = Integer.MAX_VALUE;
46     float theFloat = Float.MAX_VALUE;
47     for (int i = 0; i < NUM_DOCS; i++){
48       Document doc = new Document();
49       doc.add(newField("theLong", String.valueOf(theLong--), Field.Store.NO, Field.Index.NOT_ANALYZED));
50       doc.add(newField("theDouble", String.valueOf(theDouble--), Field.Store.NO, Field.Index.NOT_ANALYZED));
51       doc.add(newField("theByte", String.valueOf(theByte--), Field.Store.NO, Field.Index.NOT_ANALYZED));
52       doc.add(newField("theShort", String.valueOf(theShort--), Field.Store.NO, Field.Index.NOT_ANALYZED));
53       doc.add(newField("theInt", String.valueOf(theInt--), Field.Store.NO, Field.Index.NOT_ANALYZED));
54       doc.add(newField("theFloat", String.valueOf(theFloat--), Field.Store.NO, Field.Index.NOT_ANALYZED));
55       writer.addDocument(doc);
56     }
57     writer.close();
58     reader = IndexReader.open(directory, true);
59   }
60
61   @Override
62   public void tearDown() throws Exception {
63     reader.close();
64     directory.close();
65     super.tearDown();
66   }
67   
68   public void testInfoStream() throws Exception {
69     try {
70       FieldCache cache = FieldCache.DEFAULT;
71       ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
72       cache.setInfoStream(new PrintStream(bos));
73       cache.getDoubles(reader, "theDouble");
74       cache.getFloats(reader, "theDouble");
75       assertTrue(bos.toString().indexOf("WARNING") != -1);
76     } finally {
77       FieldCache.DEFAULT.purgeAllCaches();
78     }
79   }
80
81   public void test() throws IOException {
82     FieldCache cache = FieldCache.DEFAULT;
83     double [] doubles = cache.getDoubles(reader, "theDouble");
84     assertSame("Second request to cache return same array", doubles, cache.getDoubles(reader, "theDouble"));
85     assertSame("Second request with explicit parser return same array", doubles, cache.getDoubles(reader, "theDouble", FieldCache.DEFAULT_DOUBLE_PARSER));
86     assertTrue("doubles Size: " + doubles.length + " is not: " + NUM_DOCS, doubles.length == NUM_DOCS);
87     for (int i = 0; i < doubles.length; i++) {
88       assertTrue(doubles[i] + " does not equal: " + (Double.MAX_VALUE - i), doubles[i] == (Double.MAX_VALUE - i));
89
90     }
91     
92     long [] longs = cache.getLongs(reader, "theLong");
93     assertSame("Second request to cache return same array", longs, cache.getLongs(reader, "theLong"));
94     assertSame("Second request with explicit parser return same array", longs, cache.getLongs(reader, "theLong", FieldCache.DEFAULT_LONG_PARSER));
95     assertTrue("longs Size: " + longs.length + " is not: " + NUM_DOCS, longs.length == NUM_DOCS);
96     for (int i = 0; i < longs.length; i++) {
97       assertTrue(longs[i] + " does not equal: " + (Long.MAX_VALUE - i), longs[i] == (Long.MAX_VALUE - i));
98
99     }
100     
101     byte [] bytes = cache.getBytes(reader, "theByte");
102     assertSame("Second request to cache return same array", bytes, cache.getBytes(reader, "theByte"));
103     assertSame("Second request with explicit parser return same array", bytes, cache.getBytes(reader, "theByte", FieldCache.DEFAULT_BYTE_PARSER));
104     assertTrue("bytes Size: " + bytes.length + " is not: " + NUM_DOCS, bytes.length == NUM_DOCS);
105     for (int i = 0; i < bytes.length; i++) {
106       assertTrue(bytes[i] + " does not equal: " + (Byte.MAX_VALUE - i), bytes[i] == (byte) (Byte.MAX_VALUE - i));
107
108     }
109     
110     short [] shorts = cache.getShorts(reader, "theShort");
111     assertSame("Second request to cache return same array", shorts, cache.getShorts(reader, "theShort"));
112     assertSame("Second request with explicit parser return same array", shorts, cache.getShorts(reader, "theShort", FieldCache.DEFAULT_SHORT_PARSER));
113     assertTrue("shorts Size: " + shorts.length + " is not: " + NUM_DOCS, shorts.length == NUM_DOCS);
114     for (int i = 0; i < shorts.length; i++) {
115       assertTrue(shorts[i] + " does not equal: " + (Short.MAX_VALUE - i), shorts[i] == (short) (Short.MAX_VALUE - i));
116
117     }
118     
119     int [] ints = cache.getInts(reader, "theInt");
120     assertSame("Second request to cache return same array", ints, cache.getInts(reader, "theInt"));
121     assertSame("Second request with explicit parser return same array", ints, cache.getInts(reader, "theInt", FieldCache.DEFAULT_INT_PARSER));
122     assertTrue("ints Size: " + ints.length + " is not: " + NUM_DOCS, ints.length == NUM_DOCS);
123     for (int i = 0; i < ints.length; i++) {
124       assertTrue(ints[i] + " does not equal: " + (Integer.MAX_VALUE - i), ints[i] == (Integer.MAX_VALUE - i));
125
126     }
127     
128     float [] floats = cache.getFloats(reader, "theFloat");
129     assertSame("Second request to cache return same array", floats, cache.getFloats(reader, "theFloat"));
130     assertSame("Second request with explicit parser return same array", floats, cache.getFloats(reader, "theFloat", FieldCache.DEFAULT_FLOAT_PARSER));
131     assertTrue("floats Size: " + floats.length + " is not: " + NUM_DOCS, floats.length == NUM_DOCS);
132     for (int i = 0; i < floats.length; i++) {
133       assertTrue(floats[i] + " does not equal: " + (Float.MAX_VALUE - i), floats[i] == (Float.MAX_VALUE - i));
134
135     }
136   }
137 }