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