Skip to content

Commit

Permalink
Merge pull request vipshop#143 from Diffblue-benchmarks/add-unit-tests
Browse files Browse the repository at this point in the history
Add unit tests for VJTOP
  • Loading branch information
calvin1978 authored Nov 21, 2018
2 parents 34d4fce + 2d4ef7a commit 2a41b56
Show file tree
Hide file tree
Showing 3 changed files with 380 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vjtop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<junit.version>4.12</junit.version>
</properties>

<dependencies>
Expand All @@ -31,6 +32,12 @@
<scope>system</scope>
<systemPath>${toolsjar}</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
249 changes: 249 additions & 0 deletions vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
package com.vip.vjtools.vjtop.util;

import com.vip.vjtools.vjtop.WarningRule.LongWarning;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.ArrayList;

public class FormatsTest {

@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void joinInput0NotNullOutputNotNull() {

// Arrange
final ArrayList<String> list = new ArrayList<String>();
final String delim = "AAAAAAAA";

// Act
final String retval = Formats.join(list, delim);

// Assert result
Assert.assertEquals("", retval);
}

@Test
public void joinInput1NullOutputNotNull() {

// Arrange
final ArrayList<String> list = new ArrayList<String>();
list.add("");
final String delim = null;

// Act
final String retval = Formats.join(list, delim);

// Assert result
Assert.assertEquals("", retval);
}

@Test
public void leftStrInputNotNullNegativeOutputStringIndexOutOfBoundsException() {

// Arrange
final String str = "!";
final int length = -536_870_911;

// Act
thrown.expect(StringIndexOutOfBoundsException.class);
Formats.leftStr(str, length);

// Method is not expected to return due to exception thrown
}

@Test
public void leftStrInputNotNullZeroOutputNotNull() {

// Arrange
final String str = "!!!!!!!!";
final int length = 0;

// Act
final String retval = Formats.leftStr(str, length);

// Assert result
Assert.assertEquals("", retval);
}

@Test
public void rightStrInputNotNullNegativeOutputStringIndexOutOfBoundsException() {

// Arrange
final String str = "!!!!!!!!";
final int length = -1_048_568;

// Act
thrown.expect(StringIndexOutOfBoundsException.class);
Formats.rightStr(str, length);

// Method is not expected to return due to exception thrown
}

@Test
public void rightStrInputNotNullPositiveOutputNotNull() {

// Arrange
final String str = "!!!!!!!!";
final int length = 2_147_221_512;

// Act
final String retval = Formats.rightStr(str, length);

// Assert result
Assert.assertEquals("!!!!!!!!", retval);
}

@Test
public void shortNameInputNotNullPositivePositiveOutputNotNull() {

// Arrange
final String str = "!";
final int length = 1;
final int rightLength = 6;

// Act
final String retval = Formats.shortName(str, length, rightLength);

// Assert result
Assert.assertEquals("!", retval);
}

@Test
public void shortNameInputNotNullPositiveZeroOutputNotNull() {

// Arrange
final String str = "!!!!!!!!!!";
final int length = 4;
final int rightLength = 0;

// Act
final String retval = Formats.shortName(str, length, rightLength);

// Assert result
Assert.assertEquals("!...", retval);
}

@Test
public void shortNameInputNotNullZeroNegativeOutputStringIndexOutOfBoundsException() {

// Arrange
final String str = "!!!!!!!!!!";
final int length = 0;
final int rightLength = -19;

// Act
thrown.expect(StringIndexOutOfBoundsException.class);
Formats.shortName(str, length, rightLength);

// Method is not expected to return due to exception thrown
}

@Test
public void toFixLengthSizeUnitInputNullOutputNotNull() {

// Arrange
final Long size = null;

// Act
final String retval = Formats.toFixLengthSizeUnit(size);

// Assert result
Assert.assertEquals("NaN", retval);
}

@Test
public void toFixLengthSizeUnitInputPositiveOutputNotNull() {

// Arrange
final Long size = 4_611_686_018_427_387_906L;

// Act
final String retval = Formats.toFixLengthSizeUnit(size);

// Assert result
Assert.assertEquals("4194304t", retval);
}

@Test
public void toMBInputNegativeOutputNotNull() {

// Arrange
final long bytes = -8L;

// Act
final String retval = Formats.toMB(bytes);

// Assert result
Assert.assertEquals("NaN", retval);
}

@Test
public void toMBInputPositiveOutputNotNull() {

// Arrange
final long bytes = 8L;

// Act
final String retval = Formats.toMB(bytes);

// Assert result
Assert.assertEquals("0m", retval);
}

@Test
public void toSizeUnitInputNegativeOutputNotNull() {

// Arrange
final Long size = -1023L;

// Act
final String retval = Formats.toSizeUnit(size);

// Assert result
Assert.assertEquals("-1023", retval);
}

@Test
public void toSizeUnitInputNullOutputNotNull() {

// Arrange
final Long size = null;

// Act
final String retval = Formats.toSizeUnit(size);

// Assert result
Assert.assertEquals("NaN", retval);
}

@Test
public void toSizeUnitInputPositiveOutputNotNull() {

// Arrange
final Long size = 45_312L;

// Act
final String retval = Formats.toSizeUnit(size);

// Assert result
Assert.assertEquals("44k", retval);
}

@Test
public void toSizeUnitWithColorInputNullNullOutputNullPointerException() {

// Arrange
final Long size = null;
final LongWarning warning = null;

// Act
thrown.expect(NullPointerException.class);
Formats.toSizeUnitWithColor(size, warning);

// Method is not expected to return due to exception thrown
}

}
124 changes: 124 additions & 0 deletions vjtop/src/test/java/com/vip/vjtools/vjtop/util/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.vip.vjtools.vjtop.util;

import com.vip.vjtools.vjtop.util.Utils;
import org.junit.Assert;
import org.junit.Test;

public class UtilsTest {

@Test
public void calcLoadInputNegativeZeroZeroOutputZero() {

// Arrange
final Long deltaCpuTime = -4_194_304L;
final long deltaUptime = 0L;
final long factor = 0L;

// Act
final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime, factor);

// Assert result
Assert.assertEquals(0.0, retval, 0.0);
}

@Test
public void calcLoadInputPositivePositiveOutputPositive() {

// Arrange
final long deltaCpuTime = 558_348_370L;
final long deltaUptime = 1L;

// Act
final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime);

// Assert result
Assert.assertEquals(0x1.a0008001p+35 /* 5.58348e+10 */, retval, 0.0);
}

@Test
public void calcLoadInputPositivePositiveZeroOutputPositiveInfinity() {

// Arrange
final Long deltaCpuTime = 4_194_304L;
final long deltaUptime = 4_611_686_018_427_387_904L;
final long factor = 0L;

// Act
final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime, factor);

// Assert result
Assert.assertEquals(Double.POSITIVE_INFINITY, retval, 0.0);
}

@Test
public void calcLoadInputPositiveZeroOutputZero() {

// Arrange
final long deltaCpuTime = 558_348_370L;
final long deltaUptime = 0L;

// Act
final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime);

// Assert result
Assert.assertEquals(0.0, retval, 0.0);
}

@Test
public void calcLoadInputPositiveZeroZeroOutputZero() {

// Arrange
final Long deltaCpuTime = 4_194_304L;
final long deltaUptime = 0L;
final long factor = 0L;

// Act
final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime, factor);

// Assert result
Assert.assertEquals(0.0, retval, 0.0);
}

@Test
public void calcLoadInputZeroZeroOutputZero() {

// Arrange
final long deltaCpuTime = 0L;
final long deltaUptime = 0L;

// Act
final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime);

// Assert result
Assert.assertEquals(0.0, retval, 0.0);
}

@Test
public void calcMemoryUtilizationInputNegativePositiveOutputNegative() {

// Arrange
final Long threadBytes = -507_680_768_073_012_547L;
final long totalBytes = 1_147_301_170_758_571_992L;

// Act
final double retval = Utils.calcMemoryUtilization(threadBytes, totalBytes);

// Assert result
Assert.assertEquals(-0x1.6200000024f82p+5 /* -44.25 */, retval, 0.0);
}

@Test
public void calcMemoryUtilizationInputPositiveZeroOutputZero() {

// Arrange
final Long threadBytes = 1L;
final long totalBytes = 0L;

// Act
final double retval = Utils.calcMemoryUtilization(threadBytes, totalBytes);

// Assert result
Assert.assertEquals(0.0, retval, 0.0);
}

}

0 comments on commit 2a41b56

Please sign in to comment.