Skip to content

Commit

Permalink
More test refactoring, duplicate removal
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 27, 2024
1 parent cd80aa8 commit 7ab0f53
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 182 deletions.
101 changes: 101 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/io/NumberOutputTest.java
@@ -1,12 +1,65 @@
package com.fasterxml.jackson.core.io;

import java.util.Random;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

class NumberOutputTest
{
@Test
void intPrinting() throws Exception
{
assertIntPrint(0);
assertIntPrint(-3);
assertIntPrint(1234);
assertIntPrint(-1234);
assertIntPrint(56789);
assertIntPrint(-56789);
assertIntPrint(999999);
assertIntPrint(-999999);
assertIntPrint(1000000);
assertIntPrint(-1000000);
assertIntPrint(10000001);
assertIntPrint(-10000001);
assertIntPrint(-100000012);
assertIntPrint(100000012);
assertIntPrint(1999888777);
assertIntPrint(-1999888777);
assertIntPrint(Integer.MAX_VALUE);
assertIntPrint(Integer.MIN_VALUE);

Random rnd = new Random(12345L);
for (int i = 0; i < 251000; ++i) {
assertIntPrint(rnd.nextInt());
}
}

@Test
void longPrinting() throws Exception
{
// First, let's just cover couple of edge cases
assertLongPrint(0L, 0);
assertLongPrint(1L, 0);
assertLongPrint(-1L, 0);
assertLongPrint(Long.MAX_VALUE, 0);
assertLongPrint(Long.MIN_VALUE, 0);
assertLongPrint(Long.MAX_VALUE-1L, 0);
assertLongPrint(Long.MIN_VALUE+1L, 0);

Random rnd = new Random(12345L);
// Bigger value space, need more iterations for long
for (int i = 0; i < 678000; ++i) {
long l = ((long) rnd.nextInt() << 32) | rnd.nextInt();
assertLongPrint(l, i);
}
}

// // // Tests for divBy1000

@Test
void divBy1000Small()
{
Expand Down Expand Up @@ -45,4 +98,52 @@ void divBy1000FullRange() {
}
}
}

/*
/**********************************************************
/* Internal methods
/**********************************************************
*/

private void assertIntPrint(int value)
{
String exp = ""+value;
String act = printToString(value);

if (!exp.equals(act)) {
assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+")");
}
String alt = NumberOutput.toString(value);
if (!exp.equals(alt)) {
assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+")");
}
}

private void assertLongPrint(long value, int index)
{
String exp = ""+value;
String act = printToString(value);

if (!exp.equals(act)) {
assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+"; number index "+index+")");
}
String alt = NumberOutput.toString(value);
if (!exp.equals(alt)) {
assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+"; number index "+index+")");
}
}

private String printToString(int value)
{
char[] buffer = new char[12];
int offset = NumberOutput.outputInt(value, buffer, 0);
return new String(buffer, 0, offset);
}

private String printToString(long value)
{
char[] buffer = new char[22];
int offset = NumberOutput.outputLong(value, buffer, 0);
return new String(buffer, 0, offset);
}
}
Expand Up @@ -6,7 +6,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

class TestCharTypes
class CharTypesTest
extends com.fasterxml.jackson.core.JUnit5TestBase
{
@Test
Expand Down
Expand Up @@ -15,7 +15,7 @@

import static org.junit.jupiter.api.Assertions.*;

class TestDelegates extends com.fasterxml.jackson.core.JUnit5TestBase
class DelegatesTest extends com.fasterxml.jackson.core.JUnit5TestBase
{
static class BogusSchema implements FormatSchema
{
Expand Down
113 changes: 0 additions & 113 deletions src/test/java/com/fasterxml/jackson/core/util/TestNumberPrinting.java

This file was deleted.

46 changes: 0 additions & 46 deletions src/test/java/com/fasterxml/jackson/core/util/TestVersionUtil.java

This file was deleted.

74 changes: 53 additions & 21 deletions src/test/java/com/fasterxml/jackson/core/util/VersionUtilTest.java
Expand Up @@ -3,6 +3,8 @@
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.json.PackageVersion;
import com.fasterxml.jackson.core.json.UTF8JsonGenerator;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -14,39 +16,69 @@
class VersionUtilTest
{
@Test
void parseVersionSimple() {
Version v = VersionUtil.parseVersion("1.2.3-SNAPSHOT", "group", "artifact");
assertEquals("group/artifact/1.2.3-SNAPSHOT", v.toFullString());
}
void versionPartParsing()
{
assertEquals(13, VersionUtil.parseVersionPart("13"));
assertEquals(27, VersionUtil.parseVersionPart("27.8"));
assertEquals(0, VersionUtil.parseVersionPart("-3"));
}

@Test
void versionParsing()
{
assertEquals(new Version(1, 2, 15, "foo", "group", "artifact"),
VersionUtil.parseVersion("1.2.15-foo", "group", "artifact"));
Version v = VersionUtil.parseVersion("1.2.3-SNAPSHOT", "group", "artifact");
assertEquals("group/artifact/1.2.3-SNAPSHOT", v.toFullString());
}

@Test
void parseVersionPartReturningPositive() {
assertEquals(66, VersionUtil.parseVersionPart("66R"));
}
assertEquals(66, VersionUtil.parseVersionPart("66R"));
}

@Test
void parseVersionReturningVersionWhereGetMajorVersionIsZero() {
Version version = VersionUtil.parseVersion("#M&+m@569P", "#M&+m@569P", "com.fasterxml.jackson.core.util.VersionUtil");
Version version = VersionUtil.parseVersion("#M&+m@569P", "#M&+m@569P", "com.fasterxml.jackson.core.util.VersionUtil");

assertEquals(0, version.getMinorVersion());
assertEquals(0, version.getPatchLevel());
assertEquals(0, version.getMajorVersion());
assertFalse(version.isSnapshot());
assertFalse(version.isUnknownVersion());
}
assertEquals(0, version.getMinorVersion());
assertEquals(0, version.getPatchLevel());
assertEquals(0, version.getMajorVersion());
assertFalse(version.isSnapshot());
assertFalse(version.isUnknownVersion());
}

@Test
void parseVersionWithEmptyStringAndEmptyString() {
Version version = VersionUtil.parseVersion("", "", "\"g2AT");

assertTrue(version.isUnknownVersion());
}
Version version = VersionUtil.parseVersion("", "", "\"g2AT");
assertTrue(version.isUnknownVersion());
}

@Test
void parseVersionWithNullAndEmptyString() {
Version version = VersionUtil.parseVersion(null, "/nUmRN)3", "");
Version version = VersionUtil.parseVersion(null, "/nUmRN)3", "");

assertFalse(version.isSnapshot());
}
assertFalse(version.isSnapshot());
}

@Test
void packageVersionMatches() {
assertEquals(PackageVersion.VERSION, VersionUtil.versionFor(UTF8JsonGenerator.class));
}

}
// [core#248]: make sure not to return `null` but `Version.unknownVersion()`
@Test
void versionForUnknownVersion() {
// expecting return version.unknownVersion() instead of null
assertEquals(Version.unknownVersion(), VersionUtil.versionFor(VersionUtilTest.class));
}

// // // Deprecated functionality

@SuppressWarnings("deprecation")
@Test
void mavenVersionParsing() {
assertEquals(new Version(1, 2, 3, "SNAPSHOT", "foo.bar", "foo-bar"),
VersionUtil.mavenVersionFor(VersionUtilTest.class.getClassLoader(), "foo.bar", "foo-bar"));
}
}

0 comments on commit 7ab0f53

Please sign in to comment.