alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.compiler;

import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.netbeans.junit.*;
import junit.textui.TestRunner;

/**
 * Test that it is indeed possible to parse regexps from ReaderCharSequence.
 * @author Jesse Glick
 */
public class ReaderCharSequenceTest extends NbTestCase {
    
    public ReaderCharSequenceTest(String name) {
        super(name);
    }
    
    public static void main(String[] args) {
        TestRunner.run(new NbTestSuite(ReaderCharSequenceTest.class));
    }

    private Reader r;
    private ReaderCharSequence s;
    private int horizon = 9999;
    protected void setUp() throws Exception {
        r = new StringReader("Hello there! I am very happy.");
        s = new ReaderCharSequence(r, 8, horizon, 1.15f);
    }
    
    public void testLength() throws Exception {
        assertEquals(horizon, s.length());
        assertEquals('H', s.charAt(0));
        assertEquals('v', s.charAt(18));
        assertEquals(horizon + 19, s.length());
        assertEquals('.', s.charAt(28));
        assertEquals(horizon + 29, s.length());
        assertEquals('\uFFFF', s.charAt(29));
        assertEquals(29, s.length());
    }
    
    public void testToString() throws Exception {
        assertEquals("Hello there! I am very happy.", s.toString());
        assertEquals(29, s.length());
    }
    
    public void testChars() throws Exception {
        assertEquals('.', s.charAt(28));
        assertEquals('v', s.charAt(18));
        assertEquals('H', s.charAt(0));
        assertEquals('\uFFFF', s.charAt(29));
        assertEquals('v', s.charAt(18));
    }
    
    public void testSub() throws Exception {
        CharSequence sub = s.subSequence(6, 11);
        assertEquals(5, sub.length());
        assertEquals('t', sub.charAt(0));
        assertEquals('e', sub.charAt(4));
        assertEquals("there", sub.toString());
        sub = s.subSequence(18, 45);
        assertEquals(27, sub.length());
        assertEquals("very happy.", sub.toString());
        assertEquals(11, sub.length());
    }
    
    private static String deadbeef(int l) {
        char[] b = new char[l];
        Arrays.fill(b, 0, l, '\uFFFF');
        return new String(b);
    }
    
    public void testPrune() throws Exception {
        CharSequence sub = s.subSequence(0, 12);
        assertEquals("Hello there!", sub.toString());
        assertEquals(12, sub.length());
        s.prune(6);
        assertEquals(deadbeef(6) + "there!", sub.toString());
        assertEquals(12, sub.length());
        sub = s.subSequence(4, 30);
        assertEquals(26, sub.length());
        assertEquals(deadbeef(2) + "there! I am very happy.", sub.toString());
        assertEquals(25, sub.length());
        s.prune(22);
        assertEquals(deadbeef(18) + " happy.", sub.toString());
        assertEquals(25, sub.length());
        s.prune(5);
        assertEquals(deadbeef(18) + " happy.", sub.toString());
        assertEquals(25, sub.length());
    }
    
    public void testRegexp() throws Exception {
        Pattern p = Pattern.compile("er[ey]?");
        Matcher m = p.matcher(s);
        assertTrue(m.find());
        assertEquals(8, m.start());
        assertEquals(11, m.end());
        assertEquals("ere", m.group());
        s.prune(11);
        assertTrue(m.find());
        assertEquals(19, m.start());
        assertEquals(22, m.end());
        assertEquals("ery", m.group());
        s.prune(22);
        long t1 = System.currentTimeMillis();
        assertFalse(m.find());
        long t2 = System.currentTimeMillis();
        System.err.println("Time to scan to end of string (horizon=" + horizon + "): " + (t2 - t1) + "msec");
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.