1 package org.apache.lucene.analysis;
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.NumericUtils;
21 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
22 import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
24 public class TestNumericTokenStream extends BaseTokenStreamTestCase {
26 static final long lvalue = 4573245871874382L;
27 static final int ivalue = 123456;
29 public void testLongStream() throws Exception {
30 final NumericTokenStream stream=new NumericTokenStream().setLongValue(lvalue);
31 // use getAttribute to test if attributes really exist, if not an IAE will be throwed
32 final CharTermAttribute termAtt = stream.getAttribute(CharTermAttribute.class);
33 final TypeAttribute typeAtt = stream.getAttribute(TypeAttribute.class);
34 for (int shift=0; shift<64; shift+=NumericUtils.PRECISION_STEP_DEFAULT) {
35 assertTrue("New token is available", stream.incrementToken());
36 assertEquals("Term is correctly encoded", NumericUtils.longToPrefixCoded(lvalue, shift), termAtt.toString());
37 assertEquals("Type correct", (shift == 0) ? NumericTokenStream.TOKEN_TYPE_FULL_PREC : NumericTokenStream.TOKEN_TYPE_LOWER_PREC, typeAtt.type());
39 assertFalse("No more tokens available", stream.incrementToken());
42 public void testIntStream() throws Exception {
43 final NumericTokenStream stream=new NumericTokenStream().setIntValue(ivalue);
44 // use getAttribute to test if attributes really exist, if not an IAE will be throwed
45 final CharTermAttribute termAtt = stream.getAttribute(CharTermAttribute.class);
46 final TypeAttribute typeAtt = stream.getAttribute(TypeAttribute.class);
47 for (int shift=0; shift<32; shift+=NumericUtils.PRECISION_STEP_DEFAULT) {
48 assertTrue("New token is available", stream.incrementToken());
49 assertEquals("Term is correctly encoded", NumericUtils.intToPrefixCoded(ivalue, shift), termAtt.toString());
50 assertEquals("Type correct", (shift == 0) ? NumericTokenStream.TOKEN_TYPE_FULL_PREC : NumericTokenStream.TOKEN_TYPE_LOWER_PREC, typeAtt.type());
52 assertFalse("No more tokens available", stream.incrementToken());
55 public void testNotInitialized() throws Exception {
56 final NumericTokenStream stream=new NumericTokenStream();
60 fail("reset() should not succeed.");
61 } catch (IllegalStateException e) {
66 stream.incrementToken();
67 fail("incrementToken() should not succeed.");
68 } catch (IllegalStateException e) {