Home | History | Annotate | Download | only in shaping
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4 *******************************************************************************
      5 *   Copyright (C) 2001-2012, International Business Machines
      6 *   Corporation and others.  All Rights Reserved.
      7 *******************************************************************************
      8 */
      9 
     10 package com.ibm.icu.dev.test.shaping;
     11 
     12 import java.lang.reflect.Method;
     13 
     14 import org.junit.Test;
     15 import org.junit.runner.RunWith;
     16 import org.junit.runners.JUnit4;
     17 
     18 import com.ibm.icu.dev.test.TestFmwk;
     19 import com.ibm.icu.text.ArabicShaping;
     20 
     21 /**
     22  * Regression test for Arabic shaping.
     23  */
     24 @RunWith(JUnit4.class)
     25 public class ArabicShapingRegTest extends TestFmwk {
     26 
     27     /* constants copied from ArabicShaping for convenience */
     28 
     29     public static final int LENGTH_GROW_SHRINK = 0;
     30     public static final int LENGTH_FIXED_SPACES_NEAR = 1;
     31     public static final int LENGTH_FIXED_SPACES_AT_END = 2;
     32     public static final int LENGTH_FIXED_SPACES_AT_BEGINNING = 3;
     33 
     34     public static final int TEXT_DIRECTION_LOGICAL = 0;
     35     public static final int TEXT_DIRECTION_VISUAL_LTR = 4;
     36 
     37     public static final int LETTERS_NOOP = 0;
     38     public static final int LETTERS_SHAPE = 8;
     39     public static final int LETTERS_SHAPE_TASHKEEL_ISOLATED = 0x18;
     40     public static final int LETTERS_UNSHAPE = 0x10;
     41 
     42     public static final int DIGITS_NOOP = 0;
     43     public static final int DIGITS_EN2AN = 0x20;
     44     public static final int DIGITS_AN2EN = 0x40;
     45     public static final int DIGITS_EN2AN_INIT_LR = 0x60;
     46     public static final int DIGITS_EN2AN_INIT_AL = 0x80;
     47 //    private static final int DIGITS_RESERVED = 0xa0;
     48 
     49     public static final int DIGIT_TYPE_AN = 0;
     50     public static final int DIGIT_TYPE_AN_EXTENDED = 0x100;
     51 
     52 
     53     @Test
     54     public void TestEquals()
     55     {
     56         ArabicShaping as1 = new ArabicShaping(LETTERS_SHAPE | TEXT_DIRECTION_VISUAL_LTR | LENGTH_FIXED_SPACES_NEAR);
     57         ArabicShaping as2 = new ArabicShaping(LETTERS_SHAPE | TEXT_DIRECTION_VISUAL_LTR | LENGTH_FIXED_SPACES_NEAR);
     58         ArabicShaping as3 = new ArabicShaping(LETTERS_UNSHAPE | TEXT_DIRECTION_LOGICAL | LENGTH_FIXED_SPACES_AT_BEGINNING);
     59 
     60         if (! as1.equals(as1)) {
     61             err("as1: " + as1 + " does not equal itself!\n");
     62         }
     63 
     64         if (! as1.equals(as2)) {
     65             err("as1: " + as1 + ", as2: " + as2 + " are not equal, but should be.\n");
     66         }
     67 
     68         if (as1.equals(as3)) {
     69             err("as1: " + as1 + ", as3: " + as3 + " are equal but should not be.\n");
     70         }
     71     }
     72 
     73     /* Tests the method
     74      *      public int shape(char[] source, int sourceStart, int sourceLength,
     75      *      char[] dest, int destStart, int destSize) throws ArabicShapingException)
     76      */
     77     @Test
     78     public void TestShape(){
     79         // Tests when
     80         //      if (sourceStart < 0 || sourceLength < 0 || sourceStart + sourceLength > source.length)
     81         // Is true
     82         ArabicShaping as = new ArabicShaping(0);
     83         char[] source = {'d','u','m','m','y'};
     84         char[] dest = {'d','u','m','m','y'};
     85         int[] negNum = {-1,-2,-5,-10,-100};
     86 
     87 
     88         for(int i=0; i<negNum.length; i++){
     89             try{
     90                 // Checks when "sourceStart < 0"
     91                 as.shape(source, negNum[i], 0, dest, 0, 0);
     92                 errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
     93                         "suppose to return an exception when 'sourceStart < 0'.");
     94             } catch(Exception e){}
     95 
     96             try{
     97                 // Checks when "sourceLength < 0"
     98                 as.shape(source, 0, negNum[i], dest, 0, 0);
     99                 errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    100                         "suppose to return an exception when 'sourceLength < 0'.");
    101             } catch(Exception e){}
    102         }
    103 
    104         // Checks when "sourceStart + sourceLength > source.length"
    105         try{
    106             as.shape(source, 3, 3, dest, 0, 0);
    107             errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    108                     "suppose to return an exception when 'sourceStart + sourceLength > source.length'.");
    109         } catch(Exception e){}
    110         try{
    111             as.shape(source, 2, 4, dest, 0, 0);
    112             errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    113                     "suppose to return an exception when 'sourceStart + sourceLength > source.length'.");
    114         } catch(Exception e){}
    115         try{
    116             as.shape(source, 1, 5, dest, 0, 0);
    117             errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    118                     "suppose to return an exception when 'sourceStart + sourceLength > source.length'.");
    119         } catch(Exception e){}
    120         try{
    121             as.shape(source, 0, 6, dest, 0, 0);
    122             errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    123                     "suppose to return an exception when 'sourceStart + sourceLength > source.length'.");
    124         } catch(Exception e){}
    125 
    126         // Checks when "if (dest == null && destSize != 0)" is true
    127         try{
    128             as.shape(source, 2, 2, null, 0, 1);
    129             errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    130                     "suppose to return an exception when 'dest == null && destSize != 0'.");
    131         } catch(Exception e){}
    132 
    133         // Checks when
    134         // if ((destSize != 0) && (destStart < 0 || destSize < 0 || destStart + destSize > dest.length))
    135         for(int i=0; i<negNum.length; i++){
    136             try{
    137                 as.shape(source, 2, 2, dest, negNum[i], 1);
    138                 errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    139                         "suppose to return an exception when " +
    140                         "(destSize != 0) && (destStart < 0 || destSize < 0 || destStart + destSize > dest.length).");
    141             } catch(Exception e){}
    142 
    143             try{
    144                 as.shape(source, 2, 2, dest, 0, negNum[i]);
    145                 errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    146                         "suppose to return an exception when " +
    147                         "(destSize != 0) && (destStart < 0 || destSize < 0 || destStart + destSize > dest.length).");
    148             } catch(Exception e){}
    149         }
    150 
    151         // Checks when "destStart + destSize > dest.length"
    152         try{
    153             as.shape(source, 2, 2, dest, 3, 3);
    154             errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    155                     "suppose to return an exception when " +
    156                     "(destSize != 0) && (destStart < 0 || destSize < 0 || destStart + destSize > dest.length).");
    157         } catch(Exception e){}
    158         try{
    159             as.shape(source, 2, 2, dest, 2, 4);
    160             errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    161                     "suppose to return an exception when " +
    162                     "(destSize != 0) && (destStart < 0 || destSize < 0 || destStart + destSize > dest.length).");
    163         } catch(Exception e){}
    164         try{
    165             as.shape(source, 2, 2, dest, 1, 5);
    166             errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    167                     "suppose to return an exception when " +
    168                     "(destSize != 0) && (destStart < 0 || destSize < 0 || destStart + destSize > dest.length).");
    169         } catch(Exception e){}
    170         try{
    171             as.shape(source, 2, 2, dest, 0, 6);
    172             errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    173                     "suppose to return an exception when " +
    174                     "(destSize != 0) && (destStart < 0 || destSize < 0 || destStart + destSize > dest.length).");
    175         } catch(Exception e){}
    176 
    177         // Tests when "throw new IllegalArgumentException("Wrong Tashkeel argument")"
    178         int[] invalid_Tashkeel = {-1000, -500, -100};
    179         for(int i=0; i < invalid_Tashkeel.length; i++){
    180             ArabicShaping arabicShape = new ArabicShaping(invalid_Tashkeel[i]);
    181             try {
    182                 arabicShape.shape(source,0,0,dest,0,1);
    183                 errln("ArabicShaping.shape(char[],int,int,char[],int,int) was " +
    184                         "suppose to return an exception for 'Wrong Tashkeel argument' for " +
    185                         "an option value of " + invalid_Tashkeel[i]);
    186             } catch (Exception e) {}
    187         }
    188     }
    189 
    190     @Test
    191     public void TestCoverage() {
    192         ArabicShaping shp = new ArabicShaping(LETTERS_SHAPE | TEXT_DIRECTION_VISUAL_LTR | LENGTH_FIXED_SPACES_NEAR);
    193 
    194         // Test ArabicShaping#toString();
    195         assertEquals("ArabicShaping#toString() failed.",
    196                 shp.toString(),
    197                 "com.ibm.icu.text.ArabicShaping@d[LamAlef spaces at near, visual, shape letters," +
    198                         " no digit shaping, standard Arabic-Indic digits]");
    199 
    200         // Test ArabicShaping#hashCode()
    201         assertEquals("ArabicShaping#hashCode() failed.", shp.hashCode(), 13);
    202     }
    203 
    204     private boolean getStaticCharacterHelperFunctionValue(String methodName, char testValue) throws Exception {
    205         Method m = ArabicShaping.class.getDeclaredMethod(methodName, Character.TYPE);
    206         m.setAccessible(true);
    207         Object returnValue = m.invoke(null, testValue);
    208 
    209         if (Integer.class.isInstance(returnValue)) {
    210             return (Integer)returnValue == 1;
    211         }
    212         return (Boolean)returnValue;
    213     }
    214 
    215     @Test
    216     public void TestHelperFunctions() throws Exception {
    217         // Test private static helper functions that are used internally:
    218 
    219         // ArabicShaping.isSeenTailFamilyChar(char)
    220         assertTrue("ArabicShaping.isSeenTailFamilyChar(char) failed.",
    221                 getStaticCharacterHelperFunctionValue("isSeenTailFamilyChar", (char)0xfeb1));
    222 
    223         // ArabicShaping.isAlefMaksouraChar(char)
    224         assertTrue("ArabicShaping.isAlefMaksouraChar(char) failed.",
    225                 getStaticCharacterHelperFunctionValue("isAlefMaksouraChar", (char)0xfeef));
    226 
    227         // ArabicShaping.isTailChar(char)
    228         assertTrue("ArabicShaping.isTailChar(char) failed.",
    229                 getStaticCharacterHelperFunctionValue("isTailChar", (char)0x200B));
    230 
    231         // ArabicShaping.isYehHamzaChar(char)
    232         assertTrue("ArabicShaping.isYehHamzaChar(char) failed.",
    233                 getStaticCharacterHelperFunctionValue("isYehHamzaChar", (char)0xfe89));
    234 
    235     }
    236 }
    237 
    238