ExampleUnitTest.java 4.07 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
package com.yidianling.tests;

import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() throws Exception {
        assertEquals(4, 2 + 2);
    }


    String[] list1 = {"17826875951", "1782687595", "178268759", "17826875", "1782687", "178268", "17826", "1782", "178", "17", "1", "17826efdg465154",
    "165e2244", "111e12345", "我的123是121223423", "我的123456789", "符合规范357545655分好尴尬", "符合规范3555分好尴尬", "订单1234", "订单12345",
    "我的QQ号是3386218996", "178 2687 5951", "1q1q1q11q", "123456789a"};

    String[] list2 = {"178****5951", "178****595", "178****59", "17****75", "17****7", "17****", "1****", "1782", "178", "17", "1", "1****efdg46****",
    "165e2244", "111e1****", "我的123是121****23", "我的123****89", "符合规范357****55分好尴尬", "符合规范3555分好尴尬", "订单1234", "订单1****",
    "我的QQ号是338****996", "178 2687 5951", "1q1q1q11q", "123****89a"};

    /**
     * 用于测试数字替换*的功能,功能与ios一样
     */
    @Test
    public void set() {
        for (int tt = 0; tt < list1.length; tt++) {
            String text = list1[tt];
            String assertString = list2[tt];
            String lastText = "";

            int numberIndex = -1;
            int numberCount = 0;
            for (int i = 0; i < text.length(); i++) {

                if (text.charAt(i) >= 48 && text.charAt(i) <= 57) {
                    numberIndex = i;
                    for (int j = i; j < text.length(); j++) {
                        if (text.charAt(j) >= 48 && text.charAt(j) <= 57) {
                            numberCount++;
                            if (j == text.length() - 1) {
                                if (numberCount >= 5) {
                                    while (i < numberIndex + numberCount / 3) {
                                        lastText += text.charAt(i++);
                                    }
                                    while (i < numberIndex + numberCount / 3 + 4) {
                                        lastText += "*";
                                        i++;
                                    }
                                    while (i <= j) {
                                        lastText += text.charAt(i++);
                                    }
                                } else {
                                    while (i <= j) {
                                        lastText += text.charAt(i++);
                                    }
                                }

                                i = j;
                                break;
                            }
                        } else {
                            if (numberCount >= 5) {
                                while (i < numberIndex + numberCount / 3) {
                                    lastText += text.charAt(i++);
                                }
                                while (i < numberIndex + numberCount / 3 + 4) {
                                    lastText += "*";
                                    i++;
                                }
                                while (i < j) {
                                    lastText += text.charAt(i++);
                                }
                            } else {
                                while (i < j) {
                                    lastText += text.charAt(i++);
                                }
                            }

                            i = j - 1;
                            numberIndex = -1;
                            numberCount = 0;
                            break;
                        }
                    }
                } else {
                    lastText += text.charAt(i);
                }
            }
            assertEquals(lastText, assertString);
        }
    }

}