001/*
002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.jtt.loop;
024
025import org.junit.*;
026
027import com.oracle.graal.jtt.*;
028
029public class LoopParseLong extends JTTTest {
030
031    @SuppressWarnings("unused")
032    public static long testShortened(String s, int radix) throws NumberFormatException {
033        long result = 0;
034        boolean negative = false;
035        int len = s.length();
036        char firstChar = s.charAt(0);
037        if (firstChar < '0') {
038            if (firstChar == '-') {
039                negative = true;
040            } else if (firstChar != '+') {
041                throw new NumberFormatException();
042            }
043            if (len == 1) {
044                throw new NumberFormatException();
045            }
046        }
047        return result;
048    }
049
050    public static long test(String s, int radix) throws NumberFormatException {
051        if (s == null) {
052            throw new NumberFormatException("null");
053        }
054        if (radix < Character.MIN_RADIX) {
055            throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX");
056        }
057        if (radix > Character.MAX_RADIX) {
058            throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX");
059        }
060        long result = 0;
061        boolean negative = false;
062        int i = 0;
063        int len = s.length();
064        long limit = -Long.MAX_VALUE;
065        long multmin;
066        int digit;
067        if (len > 0) {
068            char firstChar = s.charAt(0);
069            if (firstChar < '0') {
070                if (firstChar == '-') {
071                    negative = true;
072                    limit = Long.MIN_VALUE;
073                } else if (firstChar != '+') {
074                    throw new NumberFormatException();
075                }
076                if (len == 1) {
077                    throw new NumberFormatException();
078                }
079                i++;
080            }
081            multmin = limit / radix;
082            while (i < len) {
083                digit = Character.digit(s.charAt(i++), radix);
084                if (digit < 0) {
085                    throw new NumberFormatException();
086                }
087                if (result < multmin) {
088                    throw new NumberFormatException();
089                }
090                result *= radix;
091                if (result < limit + digit) {
092                    throw new NumberFormatException();
093                }
094                result -= digit;
095            }
096        } else {
097            throw new NumberFormatException();
098        }
099        return negative ? result : -result;
100    }
101
102    @Test
103    public void run0() throws Throwable {
104        runTest("testShortened", "7", 10);
105    }
106
107    @Test
108    public void run1() throws Throwable {
109        runTest("testShortened", "-100", 10);
110    }
111
112    @Test
113    public void run2() throws Throwable {
114        runTest("test", "7", 10);
115    }
116
117    @Test
118    public void run3() throws Throwable {
119        runTest("test", "-100", 10);
120    }
121}