Path: blob/master/test/jdk/java/io/StreamTokenizer/Comment.java
41152 views
/*1* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 4106810 4027740 4078997 409773825@summary Make sure StreamTokenizer will correctly26parse different types of comments in input.27*/282930import java.io.*;3132public class Comment {3334public static void main(String[] args) throws Exception {3536File f = new File(System.getProperty("test.src", "."), "input.txt");3738int slashIsCommentStart = 1;39int slashSlashComment = 2;40int slashStarComment = 4;4142for (int i = 0; i < 8 ; i++) {43FileReader reader = new FileReader(f);44try {45StreamTokenizer st = new StreamTokenizer(reader);4647/* decide the state of this run */48boolean slashCommentFlag = ((i & slashIsCommentStart) != 0);49boolean slashSlashCommentFlag = ((i & slashSlashComment) != 0);50boolean slashStarCommentFlag = ((i & slashStarComment) != 0);5152/* set the initial state of the tokenizer */53if (!slashCommentFlag) {54st.ordinaryChar('/');55}56st.slashSlashComments(slashSlashCommentFlag);57st.slashStarComments(slashStarCommentFlag);5859/* now go throgh the input file */60while(st.nextToken() != StreamTokenizer.TT_EOF)61{62String token = st.sval;63if (token == null) {64continue;65} else {66if ((token.compareTo("Error1") == 0) && slashStarCommentFlag) {67throw new Exception("Failed to pass one line C comments!");68}69if ((token.compareTo("Error2") == 0) && slashStarCommentFlag) {70throw new Exception("Failed to pass multi line C comments!");71}72if ((token.compareTo("Error3") == 0) && slashSlashCommentFlag) {73throw new Exception("Failed to pass C++ comments!");74}75if ((token.compareTo("Error4") == 0) && slashCommentFlag) {76throw new Exception("Failed to pass / comments!");77}78}79}80} finally {81reader.close();82}83}84}85}868788