首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Java Format +验证Google Java格式组合不工作Gradle

Google Java Format +验证Google Java格式组合不工作Gradle
EN

Stack Overflow用户
提问于 2020-06-21 02:55:26
回答 1查看 626关注 0票数 0

我是gradle的新手;我只是将它添加到我的main repository中,看看它是如何工作的。下面是我的build.gradle文件的外观:

代码语言:javascript
复制
plugins {
    id 'java'

    id 'application'

    id 'com.github.sherter.google-java-format' version '0.8'
}

repositories {
    jcenter()
}

dependencies {
    compile fileTree(include: ['jblas-1.2.4.jar'], dir: 'libs')

    implementation 'com.google.guava:guava:28.1-jre'

    testImplementation 'junit:junit:4.12'
}

application {
    mainClassName = 'sorting.bubblesort.BubbleSort'
}

如您所见,我应用了一个名为google-java-format的插件。然而,我在这两个任务中遇到了一个问题:

代码语言:javascript
复制
googleJavaFormat
verifyGoogleJavaFormat

我可以运行googleJavaFormat,它显示它运行成功。然而,在那之后,verifyGoogleJavaFormat说它的格式不正确。下面是唯一一个格式不正确的文件:

代码语言:javascript
复制
package DataStructures.MinPriorityQueue;

import datastructures.minpriorityqueue.MinHeap.MinHeap;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;

import static org.junit.Assert.*;

public class MinHeapTest {
  @Test
  public void containsTest() {
    for (int i = 0; i < 10000; i++) {
      HashSet<Integer> set = new HashSet<>();
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      for (int j = 0; j < 1000; j++)
        if (Math.random() >= 0.5) {
          set.add(j);
          heap.add(j);
        }
      for (Integer integer : set) assertTrue(heap.contains(integer));
    }
  }

  @Test
  public void isEmptyTest() {
    for (int i = 0; i < 100; i++) {
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      assertTrue(heap.isEmpty());
      int randomAmount = 1 + (int) (Math.random() * 1000);
      for (int j = 0; j < randomAmount; j++) heap.add((int) (Math.random() * 1000));
      assertFalse(heap.isEmpty());
    }
  }

  @Test
  public void extractMinTest() {
    for (int i = 0; i < 1000; i++) {
      PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Integer::compare);
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      for (int j = 0; j < 1000; j++)
        if (Math.random() >= 0.5) {
          priorityQueue.add(j);
          heap.add(j);
        }
      assertEquals(priorityQueue.poll(), heap.extractMin());
    }
  }

  @Test
  public void chainedExtractMinTest() {
    for (int i = 0; i < 1000; i++) {
      int[] toAdd = new int[1000];
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      for (int j = 0; j < toAdd.length; j++) {
        toAdd[j] = (int) (Math.random() * 1000);
        heap.add(toAdd[j]);
      }
      int[] sorted = toAdd.clone();
      Arrays.sort(sorted);
      int[] heapValues = new int[1000];
      for (int j = 0; j < heapValues.length; j++) heapValues[j] = heap.extractMin();
      assertArrayEquals(sorted, heapValues);
    }
  }

  @Test
  public void decreaseKeyTest() {
    for (int i = 0; i < 1000; i++) {
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      PriorityQueue<Integer> queue = new PriorityQueue<>(Integer::compare);
      for (int j = 0; j < 1000; j++) heap.add(j);
      for (Integer j : heap) heap.decreaseValue(j, (int) (Math.random() * j));
      for (Integer j : heap) queue.add(j);
      while (!heap.isEmpty()) assertEquals(queue.poll(), heap.extractMin());
    }
  }
}

即使在我对这个文件运行googleJavaFormat之后,verifyGoogleJavaFormat也没有成功。为什么会发生这种情况?

EN

回答 1

Stack Overflow用户

发布于 2020-06-21 03:28:46

好了,我得到答案了。因为我使用的是Intellij,所以它在导入中移动。例如,每当它在导入中看到星号*时,它就会将其展开。我刚刚打开了File | Settings | Editor | General | Auto Import,并取消选中optimize imports on the fly。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62490331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档