[AutoDetectIndent] moved tests from platform-tests to java-tests, since formatting model builder is used. Fixed one failing - do not process blocks for comments

This commit is contained in:
Yaroslav Lepenkin
2015-06-15 15:02:41 +03:00
parent 3972b9b3d4
commit 3b1c3bad64
15 changed files with 112 additions and 152 deletions
@@ -0,0 +1,114 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class Percolation {
private final WeightedQuickUnionUF smartField;
private final int N;
private boolean[][] isOpened;
private boolean[] connectedToBottom;
private boolean myPercolates = false;
public Percolation(int N) {
if (N <= 0) {
throw new IllegalArgumentException();
}
this.N = N;
isOpened = new boolean[N + 2][N + 2];
for (int i = 0; i < N + 2; i++) {
for (int j = 0; j < N + 2; j++) {
isOpened[i][j] = false;
}
}
smartField = new WeightedQuickUnionUF(N * N + 2);
for (int i = 1; i <= N; i++) {
smartField.union(0, i);
}
connectedToBottom = new boolean[N * N + 1];
for (int i = N * (N - 1) + 1; i <= N * N; i++) {
connectedToBottom[smartField.find(i)] = true;
}
}
public void open(int i, int j) {
assertBounds(i, j);
isOpened[i][j] = true;
int smartCoord = getPlainCoordinates(i, j);
if (isOpened[i - 1][j]) unionGroups(smartCoord, smartCoord - N);
if (isOpened[i + 1][j]) unionGroups(smartCoord, smartCoord + N);
if (isOpened[i][j - 1]) unionGroups(smartCoord, smartCoord - 1);
if (isOpened[i][j + 1]) unionGroups(smartCoord, smartCoord + 1);
if (connectedToBottom(smartField.find(smartCoord)) && isFull(i, j)) {
myPercolates = true;
}
}
private void unionGroups(int first, int second) {
int firstGroupId = smartField.find(first);
int secondGroupId = smartField.find(second);
if (connectedToBottom(firstGroupId)) {
setConnectedToBottom(secondGroupId);
}
else if (connectedToBottom(secondGroupId)) {
setConnectedToBottom(firstGroupId);
}
smartField.union(first, second);
}
private void setConnectedToBottom(int secondGroupId) {
connectedToBottom[secondGroupId] = true;
}
private boolean connectedToBottom(int firstGroupId) {
return connectedToBottom[firstGroupId];
}
public boolean isOpen(int i, int j) {
assertBounds(i, j);
return isOpened[i][j];
}
public boolean isFull(int i, int j) {
assertBounds(i, j);
int coord = getPlainCoordinates(i, j);
return isOpen(i, j) && smartField.connected(0, coord);
}
private int getPlainCoordinates(int i, int j) {
return (i - 1) * N + j;
}
public boolean percolates() {
return myPercolates;
}
private void assertBounds(int i, int j) {
boolean inBounds = (i > 0 && i <= N) && (j > 0 && j <= N);
if (!inBounds) {
throw new IndexOutOfBoundsException("i: " + i + " j: " + j + " max N: " + N);
}
}
}
@@ -0,0 +1,114 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class Percolation {
private final WeightedQuickUnionUF smartField;
private final int N;
private boolean[][] isOpened;
private boolean[] connectedToBottom;
private boolean myPercolates = false;
public Percolation(int N) {
if (N <= 0) {
throw new IllegalArgumentException();
}
this.N = N;
isOpened = new boolean[N + 2][N + 2];
for (int i = 0; i < N + 2; i++) {
for (int j = 0; j < N + 2; j++) {
isOpened[i][j] = false;
}
}
smartField = new WeightedQuickUnionUF(N * N + 2);
for (int i = 1; i <= N; i++) {
smartField.union(0, i);
}
connectedToBottom = new boolean[N * N + 1];
for (int i = N * (N - 1) + 1; i <= N * N; i++) {
connectedToBottom[smartField.find(i)] = true;
}
}
public void open(int i, int j) {
assertBounds(i, j);
isOpened[i][j] = true;
int smartCoord = getPlainCoordinates(i, j);
if (isOpened[i - 1][j]) unionGroups(smartCoord, smartCoord - N);
if (isOpened[i + 1][j]) unionGroups(smartCoord, smartCoord + N);
if (isOpened[i][j - 1]) unionGroups(smartCoord, smartCoord - 1);
if (isOpened[i][j + 1]) unionGroups(smartCoord, smartCoord + 1);
if (connectedToBottom(smartField.find(smartCoord)) && isFull(i, j)) {
myPercolates = true;
}
}
private void unionGroups(int first, int second) {
int firstGroupId = smartField.find(first);
int secondGroupId = smartField.find(second);
if (connectedToBottom(firstGroupId)) {
setConnectedToBottom(secondGroupId);
}
else if (connectedToBottom(secondGroupId)) {
setConnectedToBottom(firstGroupId);
}
smartField.union(first, second);
}
private void setConnectedToBottom(int secondGroupId) {
connectedToBottom[secondGroupId] = true;
}
private boolean connectedToBottom(int firstGroupId) {
return connectedToBottom[firstGroupId];
}
public boolean isOpen(int i, int j) {
assertBounds(i, j);
return isOpened[i][j];
}
public boolean isFull(int i, int j) {
assertBounds(i, j);
int coord = getPlainCoordinates(i, j);
return isOpen(i, j) && smartField.connected(0, coord);
}
private int getPlainCoordinates(int i, int j) {
return (i - 1) * N + j;
}
public boolean percolates() {
return myPercolates;
}
private void assertBounds(int i, int j) {
boolean inBounds = (i > 0 && i <= N) && (j > 0 && j <= N);
if (!inBounds) {
throw new IndexOutOfBoundsException("i: " + i + " j: " + j + " max N: " + N);
}
}
}
@@ -0,0 +1,114 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class Percolation {
private final WeightedQuickUnionUF smartField;
private final int N;
private boolean[][] isOpened;
private boolean[] connectedToBottom;
private boolean myPercolates = false;
public Percolation(int N) {
if (N <= 0) {
throw new IllegalArgumentException();
}
this.N = N;
isOpened = new boolean[N + 2][N + 2];
for (int i = 0; i < N + 2; i++) {
for (int j = 0; j < N + 2; j++) {
isOpened[i][j] = false;
}
}
smartField = new WeightedQuickUnionUF(N * N + 2);
for (int i = 1; i <= N; i++) {
smartField.union(0, i);
}
connectedToBottom = new boolean[N * N + 1];
for (int i = N * (N - 1) + 1; i <= N * N; i++) {
connectedToBottom[smartField.find(i)] = true;
}
}
public void open(int i, int j) {
assertBounds(i, j);
isOpened[i][j] = true;
int smartCoord = getPlainCoordinates(i, j);
if (isOpened[i - 1][j]) unionGroups(smartCoord, smartCoord - N);
if (isOpened[i + 1][j]) unionGroups(smartCoord, smartCoord + N);
if (isOpened[i][j - 1]) unionGroups(smartCoord, smartCoord - 1);
if (isOpened[i][j + 1]) unionGroups(smartCoord, smartCoord + 1);
if (connectedToBottom(smartField.find(smartCoord)) && isFull(i, j)) {
myPercolates = true;
}
}
private void unionGroups(int first, int second) {
int firstGroupId = smartField.find(first);
int secondGroupId = smartField.find(second);
if (connectedToBottom(firstGroupId)) {
setConnectedToBottom(secondGroupId);
}
else if (connectedToBottom(secondGroupId)) {
setConnectedToBottom(firstGroupId);
}
smartField.union(first, second);
}
private void setConnectedToBottom(int secondGroupId) {
connectedToBottom[secondGroupId] = true;
}
private boolean connectedToBottom(int firstGroupId) {
return connectedToBottom[firstGroupId];
}
public boolean isOpen(int i, int j) {
assertBounds(i, j);
return isOpened[i][j];
}
public boolean isFull(int i, int j) {
assertBounds(i, j);
int coord = getPlainCoordinates(i, j);
return isOpen(i, j) && smartField.connected(0, coord);
}
private int getPlainCoordinates(int i, int j) {
return (i - 1) * N + j;
}
public boolean percolates() {
return myPercolates;
}
private void assertBounds(int i, int j) {
boolean inBounds = (i > 0 && i <= N) && (j > 0 && j <= N);
if (!inBounds) {
throw new IndexOutOfBoundsException("i: " + i + " j: " + j + " max N: " + N);
}
}
}
@@ -0,0 +1,83 @@
public class ChangeFileTypeRequest extends BaseRequest {
private String projectID;
private String fileID;
private String newFileType;
private String userId;
private String userName;
private String userSSN;
private String commonName;
private String newName;
private String newProjectId;
private String newFileId;
private String oldSertificate;
private String oldName;
private String oldProjectId;
private String oldMan;
private String fishEye;
private String potatoShoes;
private String marsShip;
private String moonProject;
private String iddqd;
private String timeStamp;
private String newYearBag;
private String oldTestName;
private String inner;
private String outer;
private String oldMan;
private String fishEye;
private String potatoShoes;
private String marsShip;
private String moonProject;
private String iddqd;
private String timeStamp;
private String newYearBag;
private String oldTestName;
private String inner;
private String outer;
private ChangeFileTypeRequest() {
}
/**
* @return the projectID
*/
public String getProjectID() {
return projectID;
}
/**
* @return the fileID
*/
public String getFileID() {
return fileID;
}
/**
* @return the newFileType
*/
public String getNewFileType() {
return newFileType;
}
/**
* @param protocolVersion
* @param requestID
* @param requestType
* @param userID
* @param projectID
* @param fileID
* @param newFileType
*/
public ChangeFileTypeRequest(Integer protocolVersion, String requestID,
String requestType, String userID, String projectID, String fileID,
String newFileType) {
super(protocolVersion, requestID, requestType, userID);
this.projectID = projectID;
this.fileID = fileID;
this.newFileType = newFileType;
}
}
@@ -0,0 +1,51 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public interface Doable {
/*
*
*
*
*
*
*
*
*
*
*
*/
public void count() {
}
/*
*
*
*
*
*
*
*
*
*
*
*/
public void test() {
}
}
@@ -0,0 +1,24 @@
# Assignment:
number = 42
opposite = true
# Conditions:
number = -42 if opposite
# Functions:
square = (x) -> x * x
# Arrays:
list = [1, 2, 3, 4, 5]
# Objects:
math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
# Existence:
alert "I knew it!" if elvis?
# Array comprehensions:
cubes = (math.cube num for num in list)
@@ -0,0 +1,31 @@
class Test {
public void test() {
// boolean function-available(string)
addFunction(decls, new FunctionImpl("function-available", XPathType.BOOLEAN,
new Parameter(XPathType.STRING, Parameter.Kind.REQUIRED)));
// node-set current()
addFunction(decls, new FunctionImpl("current", XPathType.NODESET));
// EXSLT (http://www.exslt.org) extensions supported by Xalan & Saxon
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("date", XPathType.STRING, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("date-time", XPathType.STRING));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("day-abbreviation", XPathType.STRING, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("day-in-month", XPathType.NUMBER, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("day-in-week", XPathType.NUMBER, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("day-in-year", XPathType.NUMBER, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("day-name", XPathType.STRING, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("day-of-week-in-month", XPathType.NUMBER, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("hour-in-day", XPathType.NUMBER, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("leap-year", XPathType.BOOLEAN, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("minute-in-hour", XPathType.NUMBER, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("month-abbreviation", XPathType.STRING, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("month-in-year", XPathType.NUMBER, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("month-name", XPathType.STRING, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("second-in-minute", XPathType.NUMBER, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("time", XPathType.STRING, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("week-in-year", XPathType.NUMBER, optional_string));
addFunction(decls, EXSLT_DATE_TIME, new FunctionImpl("year", XPathType.NUMBER, optional_string));
}
}
@@ -0,0 +1,8 @@
package aaa.bbb;
/**
* Created in IntelliJ IDEA.
* And some more text.
*/
public class Margin {
}
@@ -0,0 +1,15 @@
<?php
namespace Zend\EventManager {
class EventManager {}
}
namespace Zend\EventManager {
/**
* @var EventManager
*/
function foo()
{
}
}
@@ -0,0 +1,14 @@
public class A {
public void test() {
int a = 2;
int c = 2;
int r = 3;
int rq = 3;
int rw = 3;
int re = 3;
}
public void a() {
}
}
@@ -0,0 +1,6 @@
public void Test {
public void run() {
}
}
@@ -0,0 +1,5 @@
public void TabTest {
public void run() {}
}
@@ -16,7 +16,14 @@
package com.intellij.psi.autodetect;
import com.intellij.JavaTestUtil;
import com.intellij.openapi.fileTypes.PlainTextLanguage;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.codeStyle.autodetect.LineIndentInfo;
import com.intellij.psi.codeStyle.autodetect.LineIndentInfoBuilder;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import java.util.List;
public class JavaAutoDetectIndentTest extends AbstractIndentAutoDetectionTest {
@@ -49,4 +56,105 @@ public class JavaAutoDetectIndentTest extends AbstractIndentAutoDetectionTest {
doTestIndentSize(4);
}
public void testBigFileWithIndent2() {
doTestIndentSize(2);
}
public void testBigFileWithIndent8() {
doTestIndentSize(8);
}
public void testBigFileWithIndent4() {
doTestIndentSize(4);
}
public void testFileWithTabs() {
doTestTabsUsed();
}
public void testSimpleIndent() {
doTestMaxUsedIndent(2, 6);
}
public void testManyComments() {
doTestMaxUsedIndent(2, 6);
}
public void testManyZeroRelativeIndent() {
doTestMaxUsedIndent(2);
}
public void testSmallFileWithIndent8() {
doTestMaxUsedIndent(8);
}
public void testSmallFileWithTabs() {
doTestTabsUsed();
}
public void testNoIndentsUseLanguageSpecificSettings() {
CommonCodeStyleSettings.IndentOptions options = new CommonCodeStyleSettings.IndentOptions();
options.USE_TAB_CHARACTER = true;
doTestTabsUsed(options);
}
public void testSpacesToNumbers() throws Exception {
String text = " i\n" +
" a\n" +
" t\n";
doTestLineToIndentMapping(text, 5, 4, 10);
}
public void testEmptyLines() throws Exception {
doTestLineToIndentMapping(" \n\n\n", -1, -1, -1);
}
public void testSpacesInSimpleClass() {
doTestLineToIndentMapping(
"public class A {\n" +
"\n" +
" public void test() {\n" +
" int a = 2;\n" +
" }\n" +
"\n" +
" public void a() {\n" +
" }\n" +
"}",
0, -1, 4, 6, 4, -1, 4, 4, 0
);
}
public void testComplexIndents() {
doTestLineToIndentMapping(
"class Test\n" +
"{\n" +
" int a;\n" +
" int b;\n" +
" \n" +
" public void test() {\n" +
" int c;\n" +
" }\n" +
" \n" +
" public void run() {\n" +
" Runnable runnable = new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
" System.out.println(\"Hello!\");\n" +
" }\n" +
" };\n" +
" }\n" +
"}",
0, 0, 2, 2, -1, 2, 4, 2, -1, 2, 4, 6, 6, 8, 6, 4, 2, 0
);
}
private static void doTestLineToIndentMapping(@NotNull CharSequence text, int... spacesForLine) {
List<LineIndentInfo> list = new LineIndentInfoBuilder(text, PlainTextLanguage.INSTANCE).build();
Assert.assertEquals(list.size(), spacesForLine.length);
for (int i = 0; i < spacesForLine.length; i++) {
int indentSize = list.get(i).getIndentSize();
Assert.assertEquals("Mismatch on line " + i, spacesForLine[i], indentSize);
}
}
}