/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.lucene.analysis.bn;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.core.KeywordTokenizer;

import java.io.IOException;

/**
 * Test BengaliNormalizer
 */
public class TestBengaliNormalizer extends BaseTokenStreamTestCase {
  /**
   * Test some basic normalization, with an example from the paper.
   */
  public void testChndrobindu() throws IOException {
    check("চাঁদ", "চাদ");
  }

  public void testRosshoIKar() throws IOException {
    check("বাড়ী", "বারি");
    check("তীর", "তির");
  }

  public void testRosshoUKar() throws IOException {
    check("ভূল", "ভুল");
    check("অনূপ", "অনুপ");
  }

  public void testNga() throws IOException {
    check("বাঙলা", "বাংলা");
  }

  public void testJaPhaala() throws IOException {
    check("ব্যাক্তি", "বেক্তি");
    check( "সন্ধ্যা", "সন্ধা");
  }

  public void testBaPhalaa() throws IOException {
    check("স্বদেশ", "সদেস");
    check("তত্ত্ব", "তত্ত");
    check("বিশ্ব", "বিসস");
  }

  public void testVisarga() throws IOException {
    check("দুঃখ", "দুখখ");
    check("উঃ", "উহ");
    check("পুনঃ", "পুন");
  }

  public void testBasics() throws IOException {
    check("কণা", "কনা");
    check("শরীর", "সরির");
    check("বাড়ি", "বারি");
  }

  private void check(String input, String output) throws IOException {
    Tokenizer tokenizer = whitespaceMockTokenizer(input);
    TokenFilter tf = new BengaliNormalizationFilter(tokenizer);
    assertTokenStreamContents(tf, new String[] { output });
  }
  
  public void testEmptyTerm() throws IOException {
    Analyzer a = new Analyzer() {
      @Override
      protected TokenStreamComponents createComponents(String fieldName) {
        Tokenizer tokenizer = new KeywordTokenizer();
        return new TokenStreamComponents(tokenizer, new BengaliNormalizationFilter(tokenizer));
      }
    };
    checkOneTerm(a, "", "");
    a.close();
  }
}

@@ -1087,16 +1087,56 @@
 kenizer;
+%0Aimport org.apache.lucene.util.TestUtil;
 %0A%0Aimport
@@ -2245,16 +2245,816 @@
 );%0A  %7D%0A%0A
+  /** creates random strings in the bengali block and ensures the normalizer doesn't trip up on them */%0A  public void testRandom() throws IOException %7B%0A    BengaliNormalizer normalizer = new BengaliNormalizer();%0A    for (int i = 0; i %3C 100000; i++) %7B%0A      String randomBengali = TestUtil.randomSimpleStringRange(random(), '%5Cu0980', '%5Cu09FF', 7);%0A      try %7B%0A        int newLen = normalizer.normalize(randomBengali.toCharArray(), randomBengali.length());%0A        assertTrue(newLen %3E= 0); // should not return negative length%0A        assertTrue(newLen %3C= randomBengali.length()); // should not increase length of string%0A      %7D catch (Exception e) %7B%0A        System.err.println(%22normalizer failed on input: '%22 + randomBengali + %22' (%22 + escape(randomBengali) + %22)%22);%0A        throw e;%0A      %7D%0A    %7D%0A  %7D%0A%0A
   privat

add more efficient (failing) test for BengaliNormalizer