repo
stringclasses 1
value | licence_name
stringclasses 1
value | sha
stringclasses 1
value | language
stringclasses 1
value | context
stringlengths 1.31k
10.9k
| CLASS_NAME
stringlengths 3
21
| METHOD_NAME
stringlengths 3
33
| TEST_FILE
stringlengths 12
30
| LIBRARIES
stringclasses 1
value | SUGGESTED_FRAMEWORK
stringclasses 1
value | SOURCES
stringlengths 727
10.3k
| CREATE_NEW_FILE
stringclasses 1
value | CUSTOM_INSTRUCTIONS
stringclasses 1
value | testCode
stringlengths 110
1.92k
| testOffset
int64 129
6.72k
| testPath
stringlengths 50
68
| codeUnderTest
stringlengths 50
2.04k
| codeUnderTestOffset
int64 285
9.81k
| codeUnderTestPath
stringlengths 46
64
| codeUnderTestType
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `equals` method in the `ByteString` class. Put new test methods inside the file `ByteStringTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: ByteString.java
```
package co.nstant.in.cbor.model;
import java.util.Arrays;
public class ByteString extends ChunkableDataItem {
private final byte[] bytes;
public ByteString(byte[] bytes) {
super(MajorType.BYTE_STRING);
if (bytes == null) {
this.bytes = null;
} else {
this.bytes = bytes;
}
}
public byte[] getBytes() {
if (bytes == null) {
return null;
} else {
return bytes;
}
}
@Override
public boolean equals(Object object) {
if (object instanceof ByteString) {
ByteString other = (ByteString) object;
return super.equals(object) && Arrays.equals(bytes, other.bytes);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Arrays.hashCode(bytes);
}
}
``` | ByteString | equals | ByteStringTest.java | JUnit4 | File: ByteString.java
```
package co.nstant.in.cbor.model;
import java.util.Arrays;
public class ByteString extends ChunkableDataItem {
private final byte[] bytes;
public ByteString(byte[] bytes) {
super(MajorType.BYTE_STRING);
if (bytes == null) {
this.bytes = null;
} else {
this.bytes = bytes;
}
}
public byte[] getBytes() {
if (bytes == null) {
return null;
} else {
return bytes;
}
}
@Override
public boolean equals(Object object) {
if (object instanceof ByteString) {
ByteString other = (ByteString) object;
return super.equals(object) && Arrays.equals(bytes, other.bytes);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Arrays.hashCode(bytes);
}
}
``` | false | @Test
public void shouldNotEquals() {
byte[] bytes = "string".getBytes();
ByteString byteString = new ByteString(bytes);
assertFalse(byteString.equals(new Object()));
} | 837 | src/test/java/co/nstant/in/cbor/model/ByteStringTest.java | @Override
public boolean equals(Object object) {
if (object instanceof ByteString) {
ByteString other = (ByteString) object;
return super.equals(object) && Arrays.equals(bytes, other.bytes);
}
return false;
} | 497 | src/main/java/co/nstant/in/cbor/model/ByteString.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `hashCode` method in the `ByteString` class. Put new test methods inside the file `ByteStringTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: ByteString.java
```
package co.nstant.in.cbor.model;
import java.util.Arrays;
public class ByteString extends ChunkableDataItem {
private final byte[] bytes;
public ByteString(byte[] bytes) {
super(MajorType.BYTE_STRING);
if (bytes == null) {
this.bytes = null;
} else {
this.bytes = bytes;
}
}
public byte[] getBytes() {
if (bytes == null) {
return null;
} else {
return bytes;
}
}
@Override
public boolean equals(Object object) {
if (object instanceof ByteString) {
ByteString other = (ByteString) object;
return super.equals(object) && Arrays.equals(bytes, other.bytes);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Arrays.hashCode(bytes);
}
}
``` | ByteString | hashCode | ByteStringTest.java | JUnit4 | File: ByteString.java
```
package co.nstant.in.cbor.model;
import java.util.Arrays;
public class ByteString extends ChunkableDataItem {
private final byte[] bytes;
public ByteString(byte[] bytes) {
super(MajorType.BYTE_STRING);
if (bytes == null) {
this.bytes = null;
} else {
this.bytes = bytes;
}
}
public byte[] getBytes() {
if (bytes == null) {
return null;
} else {
return bytes;
}
}
@Override
public boolean equals(Object object) {
if (object instanceof ByteString) {
ByteString other = (ByteString) object;
return super.equals(object) && Arrays.equals(bytes, other.bytes);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Arrays.hashCode(bytes);
}
}
``` | false | @Test
public void shouldHashcode() {
ChunkableDataItem superClass = new ChunkableDataItem(MajorType.BYTE_STRING);
byte[] bytes = "string".getBytes();
ByteString byteString = new ByteString(bytes);
assertEquals(byteString.hashCode(), superClass.hashCode() ^ Arrays.hashCode(bytes));
} | 1,043 | src/test/java/co/nstant/in/cbor/model/ByteStringTest.java | @Override
public int hashCode() {
return super.hashCode() ^ Arrays.hashCode(bytes);
} | 767 | src/main/java/co/nstant/in/cbor/model/ByteString.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `getBytes` method in the `ByteString` class. Put new test methods inside the file `ByteStringTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: ByteString.java
```
package co.nstant.in.cbor.model;
import java.util.Arrays;
public class ByteString extends ChunkableDataItem {
private final byte[] bytes;
public ByteString(byte[] bytes) {
super(MajorType.BYTE_STRING);
if (bytes == null) {
this.bytes = null;
} else {
this.bytes = bytes;
}
}
public byte[] getBytes() {
if (bytes == null) {
return null;
} else {
return bytes;
}
}
@Override
public boolean equals(Object object) {
if (object instanceof ByteString) {
ByteString other = (ByteString) object;
return super.equals(object) && Arrays.equals(bytes, other.bytes);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Arrays.hashCode(bytes);
}
}
``` | ByteString | getBytes | ByteStringTest.java | JUnit4 | File: ByteString.java
```
package co.nstant.in.cbor.model;
import java.util.Arrays;
public class ByteString extends ChunkableDataItem {
private final byte[] bytes;
public ByteString(byte[] bytes) {
super(MajorType.BYTE_STRING);
if (bytes == null) {
this.bytes = null;
} else {
this.bytes = bytes;
}
}
public byte[] getBytes() {
if (bytes == null) {
return null;
} else {
return bytes;
}
}
@Override
public boolean equals(Object object) {
if (object instanceof ByteString) {
ByteString other = (ByteString) object;
return super.equals(object) && Arrays.equals(bytes, other.bytes);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Arrays.hashCode(bytes);
}
}
``` | false | @Test
public void shouldNotClone() {
byte[] bytes = "see issue #18".getBytes();
ByteString byteString = new ByteString(bytes);
assertEquals(byteString.getBytes(), bytes);
} | 1,372 | src/test/java/co/nstant/in/cbor/model/ByteStringTest.java | public byte[] getBytes() {
if (bytes == null) {
return null;
} else {
return bytes;
}
} | 352 | src/main/java/co/nstant/in/cbor/model/ByteString.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `setTag` method in the `DataItem` class. Put new test methods inside the file `DataItemTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: DataItem.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DataItem {
private final MajorType majorType;
private Tag tag;
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType() {
return majorType;
}
public void setTag(long tag) {
if (tag < 0) {
throw new IllegalArgumentException("tag number must be 0 or greater");
}
this.tag = new Tag(tag);
}
public void setTag(Tag tag) {
Objects.requireNonNull(tag, "tag is null");
this.tag = tag;
}
public void removeTag() {
tag = null;
}
public Tag getTag() {
return tag;
}
public boolean hasTag() {
return tag != null;
}
@Override
public boolean equals(Object object) {
if (object instanceof DataItem) {
DataItem other = (DataItem) object;
if (tag != null) {
return tag.equals(other.tag) && majorType == other.majorType;
} else {
return other.tag == null && majorType == other.majorType;
}
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(majorType, tag);
}
protected void assertTrue(boolean condition, String message) {
if (!condition) {
throw new IllegalArgumentException(message);
}
}
public DataItem getOuterTaggable() {
DataItem item = this;
while (item.getTag() != null) {
item = item.getTag();
}
return item;
}
}
```
File: Tag.java
Package: co.nstant.in.cbor.model
```
public class Tag extends DataItem {
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | DataItem | setTag | DataItemTest.java | JUnit4 | File: DataItem.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DataItem {
private final MajorType majorType;
private Tag tag;
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType() {
return majorType;
}
public void setTag(long tag) {
if (tag < 0) {
throw new IllegalArgumentException("tag number must be 0 or greater");
}
this.tag = new Tag(tag);
}
public void setTag(Tag tag) {
Objects.requireNonNull(tag, "tag is null");
this.tag = tag;
}
public void removeTag() {
tag = null;
}
public Tag getTag() {
return tag;
}
public boolean hasTag() {
return tag != null;
}
@Override
public boolean equals(Object object) {
if (object instanceof DataItem) {
DataItem other = (DataItem) object;
if (tag != null) {
return tag.equals(other.tag) && majorType == other.majorType;
} else {
return other.tag == null && majorType == other.majorType;
}
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(majorType, tag);
}
protected void assertTrue(boolean condition, String message) {
if (!condition) {
throw new IllegalArgumentException(message);
}
}
public DataItem getOuterTaggable() {
DataItem item = this;
while (item.getTag() != null) {
item = item.getTag();
}
return item;
}
}
```
File: Tag.java
Package: co.nstant.in.cbor.model
```
public class Tag extends DataItem {
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test(expected = NullPointerException.class)
public void testSetTag_Tag_null() {
DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER);
di.setTag(null);
} | 1,141 | src/test/java/co/nstant/in/cbor/model/DataItemTest.java | public void setTag(Tag tag) {
Objects.requireNonNull(tag, "tag is null");
this.tag = tag;
} | 567 | src/main/java/co/nstant/in/cbor/model/DataItem.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `setTag` method in the `DataItem` class. Put new test methods inside the file `DataItemTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: DataItem.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DataItem {
private final MajorType majorType;
private Tag tag;
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType() {
return majorType;
}
public void setTag(long tag) {
if (tag < 0) {
throw new IllegalArgumentException("tag number must be 0 or greater");
}
this.tag = new Tag(tag);
}
public void setTag(Tag tag) {
Objects.requireNonNull(tag, "tag is null");
this.tag = tag;
}
public void removeTag() {
tag = null;
}
public Tag getTag() {
return tag;
}
public boolean hasTag() {
return tag != null;
}
@Override
public boolean equals(Object object) {
if (object instanceof DataItem) {
DataItem other = (DataItem) object;
if (tag != null) {
return tag.equals(other.tag) && majorType == other.majorType;
} else {
return other.tag == null && majorType == other.majorType;
}
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(majorType, tag);
}
protected void assertTrue(boolean condition, String message) {
if (!condition) {
throw new IllegalArgumentException(message);
}
}
public DataItem getOuterTaggable() {
DataItem item = this;
while (item.getTag() != null) {
item = item.getTag();
}
return item;
}
}
```
File: Tag.java
Package: co.nstant.in.cbor.model
```
public class Tag extends DataItem {
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | DataItem | setTag | DataItemTest.java | JUnit4 | File: DataItem.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DataItem {
private final MajorType majorType;
private Tag tag;
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType() {
return majorType;
}
public void setTag(long tag) {
if (tag < 0) {
throw new IllegalArgumentException("tag number must be 0 or greater");
}
this.tag = new Tag(tag);
}
public void setTag(Tag tag) {
Objects.requireNonNull(tag, "tag is null");
this.tag = tag;
}
public void removeTag() {
tag = null;
}
public Tag getTag() {
return tag;
}
public boolean hasTag() {
return tag != null;
}
@Override
public boolean equals(Object object) {
if (object instanceof DataItem) {
DataItem other = (DataItem) object;
if (tag != null) {
return tag.equals(other.tag) && majorType == other.majorType;
} else {
return other.tag == null && majorType == other.majorType;
}
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(majorType, tag);
}
protected void assertTrue(boolean condition, String message) {
if (!condition) {
throw new IllegalArgumentException(message);
}
}
public DataItem getOuterTaggable() {
DataItem item = this;
while (item.getTag() != null) {
item = item.getTag();
}
return item;
}
}
```
File: Tag.java
Package: co.nstant.in.cbor.model
```
public class Tag extends DataItem {
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void testGetTag() {
DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER);
di.setTag(new Tag(1));
Tag t = di.getTag();
assertEquals(1L, t.getValue());
} | 1,326 | src/test/java/co/nstant/in/cbor/model/DataItemTest.java | public void setTag(Tag tag) {
Objects.requireNonNull(tag, "tag is null");
this.tag = tag;
} | 567 | src/main/java/co/nstant/in/cbor/model/DataItem.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `setTag` method in the `DataItem` class. Put new test methods inside the file `DataItemTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: DataItem.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DataItem {
private final MajorType majorType;
private Tag tag;
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType() {
return majorType;
}
public void setTag(long tag) {
if (tag < 0) {
throw new IllegalArgumentException("tag number must be 0 or greater");
}
this.tag = new Tag(tag);
}
public void setTag(Tag tag) {
Objects.requireNonNull(tag, "tag is null");
this.tag = tag;
}
public void removeTag() {
tag = null;
}
public Tag getTag() {
return tag;
}
public boolean hasTag() {
return tag != null;
}
@Override
public boolean equals(Object object) {
if (object instanceof DataItem) {
DataItem other = (DataItem) object;
if (tag != null) {
return tag.equals(other.tag) && majorType == other.majorType;
} else {
return other.tag == null && majorType == other.majorType;
}
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(majorType, tag);
}
protected void assertTrue(boolean condition, String message) {
if (!condition) {
throw new IllegalArgumentException(message);
}
}
public DataItem getOuterTaggable() {
DataItem item = this;
while (item.getTag() != null) {
item = item.getTag();
}
return item;
}
}
```
File: Tag.java
Package: co.nstant.in.cbor.model
```
public class Tag extends DataItem {
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | DataItem | setTag | DataItemTest.java | JUnit4 | File: DataItem.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DataItem {
private final MajorType majorType;
private Tag tag;
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType() {
return majorType;
}
public void setTag(long tag) {
if (tag < 0) {
throw new IllegalArgumentException("tag number must be 0 or greater");
}
this.tag = new Tag(tag);
}
public void setTag(Tag tag) {
Objects.requireNonNull(tag, "tag is null");
this.tag = tag;
}
public void removeTag() {
tag = null;
}
public Tag getTag() {
return tag;
}
public boolean hasTag() {
return tag != null;
}
@Override
public boolean equals(Object object) {
if (object instanceof DataItem) {
DataItem other = (DataItem) object;
if (tag != null) {
return tag.equals(other.tag) && majorType == other.majorType;
} else {
return other.tag == null && majorType == other.majorType;
}
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(majorType, tag);
}
protected void assertTrue(boolean condition, String message) {
if (!condition) {
throw new IllegalArgumentException(message);
}
}
public DataItem getOuterTaggable() {
DataItem item = this;
while (item.getTag() != null) {
item = item.getTag();
}
return item;
}
}
```
File: Tag.java
Package: co.nstant.in.cbor.model
```
public class Tag extends DataItem {
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void testRemoveTag() {
DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER);
di.setTag(new Tag(1));
assertNotNull(di.getTag());
di.removeTag();
assertNull(di.getTag());
} | 1,750 | src/test/java/co/nstant/in/cbor/model/DataItemTest.java | public void setTag(Tag tag) {
Objects.requireNonNull(tag, "tag is null");
this.tag = tag;
} | 567 | src/main/java/co/nstant/in/cbor/model/DataItem.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `getLanguage` method in the `LanguageTaggedString` class. Put new test methods inside the file `LanguageTaggedStringTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: LanguageTaggedString.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class LanguageTaggedString extends Array {
public LanguageTaggedString(String language, String string) {
this(new UnicodeString(language), new UnicodeString(string));
}
public LanguageTaggedString(UnicodeString language, UnicodeString string) {
setTag(38);
add(Objects.requireNonNull(language));
add(Objects.requireNonNull(string));
}
public UnicodeString getLanguage() {
return (UnicodeString) getDataItems().get(0);
}
public UnicodeString getString() {
return (UnicodeString) getDataItems().get(1);
}
}
```
File: UnicodeString.java
Package: co.nstant.in.cbor.model
```
public class UnicodeString extends ChunkableDataItem {
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString();
public String getString();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | LanguageTaggedString | getLanguage | LanguageTaggedStringTest.java | JUnit4 | File: LanguageTaggedString.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class LanguageTaggedString extends Array {
public LanguageTaggedString(String language, String string) {
this(new UnicodeString(language), new UnicodeString(string));
}
public LanguageTaggedString(UnicodeString language, UnicodeString string) {
setTag(38);
add(Objects.requireNonNull(language));
add(Objects.requireNonNull(string));
}
public UnicodeString getLanguage() {
return (UnicodeString) getDataItems().get(0);
}
public UnicodeString getString() {
return (UnicodeString) getDataItems().get(1);
}
}
```
File: UnicodeString.java
Package: co.nstant.in.cbor.model
```
public class UnicodeString extends ChunkableDataItem {
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString();
public String getString();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void shouldInitializeWithStrings() {
LanguageTaggedString lts = new LanguageTaggedString("en", "Hello");
assertEquals(38, lts.getTag().getValue());
assertEquals("en", lts.getLanguage().getString());
assertEquals("Hello", lts.getString().getString());
} | 149 | src/test/java/co/nstant/in/cbor/model/LanguageTaggedStringTest.java | public UnicodeString getLanguage() {
return (UnicodeString) getDataItems().get(0);
} | 520 | src/main/java/co/nstant/in/cbor/model/LanguageTaggedString.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `encode` method in the `CborEncoder` class. Put new test methods inside the file `CborEncoderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborEncoder.java
```
package co.nstant.in.cbor;
import java.io.OutputStream;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.encoder.ArrayEncoder;
import co.nstant.in.cbor.encoder.ByteStringEncoder;
import co.nstant.in.cbor.encoder.MapEncoder;
import co.nstant.in.cbor.encoder.NegativeIntegerEncoder;
import co.nstant.in.cbor.encoder.SpecialEncoder;
import co.nstant.in.cbor.encoder.TagEncoder;
import co.nstant.in.cbor.encoder.UnicodeStringEncoder;
import co.nstant.in.cbor.encoder.UnsignedIntegerEncoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.ByteString;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
import co.nstant.in.cbor.model.NegativeInteger;
import co.nstant.in.cbor.model.SimpleValue;
import co.nstant.in.cbor.model.Special;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
import co.nstant.in.cbor.model.UnsignedInteger;
public class CborEncoder {
private final UnsignedIntegerEncoder unsignedIntegerEncoder;
private final NegativeIntegerEncoder negativeIntegerEncoder;
private final ByteStringEncoder byteStringEncoder;
private final UnicodeStringEncoder unicodeStringEncoder;
private final ArrayEncoder arrayEncoder;
private final MapEncoder mapEncoder;
private final TagEncoder tagEncoder;
private final SpecialEncoder specialEncoder;
private boolean canonical = true;
public CborEncoder(OutputStream outputStream) {
Objects.requireNonNull(outputStream);
unsignedIntegerEncoder = new UnsignedIntegerEncoder(this, outputStream);
negativeIntegerEncoder = new NegativeIntegerEncoder(this, outputStream);
byteStringEncoder = new ByteStringEncoder(this, outputStream);
unicodeStringEncoder = new UnicodeStringEncoder(this, outputStream);
arrayEncoder = new ArrayEncoder(this, outputStream);
mapEncoder = new MapEncoder(this, outputStream);
tagEncoder = new TagEncoder(this, outputStream);
specialEncoder = new SpecialEncoder(this, outputStream);
}
public void encode(List<DataItem> dataItems) throws CborException {
for (DataItem dataItem : dataItems) {
encode(dataItem);
}
}
public void encode(DataItem dataItem) throws CborException {
if (dataItem == null) {
dataItem = SimpleValue.NULL;
}
if (dataItem.hasTag()) {
Tag tagDi = dataItem.getTag();
encode(tagDi);
}
switch (dataItem.getMajorType()) {
case UNSIGNED_INTEGER:
unsignedIntegerEncoder.encode((UnsignedInteger) dataItem);
break;
case NEGATIVE_INTEGER:
negativeIntegerEncoder.encode((NegativeInteger) dataItem);
break;
case BYTE_STRING:
byteStringEncoder.encode((ByteString) dataItem);
break;
case UNICODE_STRING:
unicodeStringEncoder.encode((UnicodeString) dataItem);
break;
case ARRAY:
arrayEncoder.encode((Array) dataItem);
break;
case MAP:
mapEncoder.encode((Map) dataItem);
break;
case SPECIAL:
specialEncoder.encode((Special) dataItem);
break;
case TAG:
tagEncoder.encode((Tag) dataItem);
break;
default:
throw new CborException("Unknown major type");
}
}
public boolean isCanonical() {
return canonical;
}
public CborEncoder nonCanonical() {
canonical = false;
return this;
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | CborEncoder | encode | CborEncoderTest.java | JUnit4 | File: CborEncoder.java
```
package co.nstant.in.cbor;
import java.io.OutputStream;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.encoder.ArrayEncoder;
import co.nstant.in.cbor.encoder.ByteStringEncoder;
import co.nstant.in.cbor.encoder.MapEncoder;
import co.nstant.in.cbor.encoder.NegativeIntegerEncoder;
import co.nstant.in.cbor.encoder.SpecialEncoder;
import co.nstant.in.cbor.encoder.TagEncoder;
import co.nstant.in.cbor.encoder.UnicodeStringEncoder;
import co.nstant.in.cbor.encoder.UnsignedIntegerEncoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.ByteString;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
import co.nstant.in.cbor.model.NegativeInteger;
import co.nstant.in.cbor.model.SimpleValue;
import co.nstant.in.cbor.model.Special;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
import co.nstant.in.cbor.model.UnsignedInteger;
public class CborEncoder {
private final UnsignedIntegerEncoder unsignedIntegerEncoder;
private final NegativeIntegerEncoder negativeIntegerEncoder;
private final ByteStringEncoder byteStringEncoder;
private final UnicodeStringEncoder unicodeStringEncoder;
private final ArrayEncoder arrayEncoder;
private final MapEncoder mapEncoder;
private final TagEncoder tagEncoder;
private final SpecialEncoder specialEncoder;
private boolean canonical = true;
public CborEncoder(OutputStream outputStream) {
Objects.requireNonNull(outputStream);
unsignedIntegerEncoder = new UnsignedIntegerEncoder(this, outputStream);
negativeIntegerEncoder = new NegativeIntegerEncoder(this, outputStream);
byteStringEncoder = new ByteStringEncoder(this, outputStream);
unicodeStringEncoder = new UnicodeStringEncoder(this, outputStream);
arrayEncoder = new ArrayEncoder(this, outputStream);
mapEncoder = new MapEncoder(this, outputStream);
tagEncoder = new TagEncoder(this, outputStream);
specialEncoder = new SpecialEncoder(this, outputStream);
}
public void encode(List<DataItem> dataItems) throws CborException {
for (DataItem dataItem : dataItems) {
encode(dataItem);
}
}
public void encode(DataItem dataItem) throws CborException {
if (dataItem == null) {
dataItem = SimpleValue.NULL;
}
if (dataItem.hasTag()) {
Tag tagDi = dataItem.getTag();
encode(tagDi);
}
switch (dataItem.getMajorType()) {
case UNSIGNED_INTEGER:
unsignedIntegerEncoder.encode((UnsignedInteger) dataItem);
break;
case NEGATIVE_INTEGER:
negativeIntegerEncoder.encode((NegativeInteger) dataItem);
break;
case BYTE_STRING:
byteStringEncoder.encode((ByteString) dataItem);
break;
case UNICODE_STRING:
unicodeStringEncoder.encode((UnicodeString) dataItem);
break;
case ARRAY:
arrayEncoder.encode((Array) dataItem);
break;
case MAP:
mapEncoder.encode((Map) dataItem);
break;
case SPECIAL:
specialEncoder.encode((Special) dataItem);
break;
case TAG:
tagEncoder.encode((Tag) dataItem);
break;
default:
throw new CborException("Unknown major type");
}
}
public boolean isCanonical() {
return canonical;
}
public CborEncoder nonCanonical() {
canonical = false;
return this;
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test(expected = ClassCastException.class)
public void shouldExpectSpecialImplementation() throws CborException {
encoder.encode(new Mock(MajorType.SPECIAL));
} | 1,996 | src/test/java/co/nstant/in/cbor/CborEncoderTest.java | /**
* Encode a single {@link DataItem}.
*
* @param dataItem the {@link DataItem} to encode. If null, encoder encodes a
* {@link SimpleValue} NULL value.
* @throws CborException if {@link DataItem} could not be encoded or there was
* an problem with the {@link OutputStream}.
*/
public void encode(DataItem dataItem) throws CborException {
if (dataItem == null) {
dataItem = SimpleValue.NULL;
}
if (dataItem.hasTag()) {
Tag tagDi = dataItem.getTag();
encode(tagDi);
}
switch (dataItem.getMajorType()) {
case UNSIGNED_INTEGER:
unsignedIntegerEncoder.encode((UnsignedInteger) dataItem);
break;
case NEGATIVE_INTEGER:
negativeIntegerEncoder.encode((NegativeInteger) dataItem);
break;
case BYTE_STRING:
byteStringEncoder.encode((ByteString) dataItem);
break;
case UNICODE_STRING:
unicodeStringEncoder.encode((UnicodeString) dataItem);
break;
case ARRAY:
arrayEncoder.encode((Array) dataItem);
break;
case MAP:
mapEncoder.encode((Map) dataItem);
break;
case SPECIAL:
specialEncoder.encode((Special) dataItem);
break;
case TAG:
tagEncoder.encode((Tag) dataItem);
break;
default:
throw new CborException("Unknown major type");
}
} | 2,804 | src/main/java/co/nstant/in/cbor/CborEncoder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `hashCode` method in the `DoublePrecisionFloat` class. Put new test methods inside the file `DoublePrecisionFloatTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: DoublePrecisionFloat.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DoublePrecisionFloat extends Special {
private final double value;
public DoublePrecisionFloat(double value) {
super(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT);
this.value = value;
}
public double getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof DoublePrecisionFloat) {
DoublePrecisionFloat other = (DoublePrecisionFloat) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return String.valueOf(value);
}
}
``` | DoublePrecisionFloat | hashCode | DoublePrecisionFloatTest.java | JUnit4 | File: DoublePrecisionFloat.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DoublePrecisionFloat extends Special {
private final double value;
public DoublePrecisionFloat(double value) {
super(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT);
this.value = value;
}
public double getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof DoublePrecisionFloat) {
DoublePrecisionFloat other = (DoublePrecisionFloat) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return String.valueOf(value);
}
}
``` | false | @Test
public void testHashcode() {
Special superClass = new Special(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT);
double value = 1.234;
DoublePrecisionFloat doublePrecisionFloat = new DoublePrecisionFloat(value);
assertEquals(superClass.hashCode() ^ Objects.hashCode(value), doublePrecisionFloat.hashCode());
} | 732 | src/test/java/co/nstant/in/cbor/model/DoublePrecisionFloatTest.java | @Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
} | 641 | src/main/java/co/nstant/in/cbor/model/DoublePrecisionFloat.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `toString` method in the `DoublePrecisionFloat` class. Put new test methods inside the file `DoublePrecisionFloatTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: DoublePrecisionFloat.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DoublePrecisionFloat extends Special {
private final double value;
public DoublePrecisionFloat(double value) {
super(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT);
this.value = value;
}
public double getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof DoublePrecisionFloat) {
DoublePrecisionFloat other = (DoublePrecisionFloat) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return String.valueOf(value);
}
}
``` | DoublePrecisionFloat | toString | DoublePrecisionFloatTest.java | JUnit4 | File: DoublePrecisionFloat.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class DoublePrecisionFloat extends Special {
private final double value;
public DoublePrecisionFloat(double value) {
super(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT);
this.value = value;
}
public double getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof DoublePrecisionFloat) {
DoublePrecisionFloat other = (DoublePrecisionFloat) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return String.valueOf(value);
}
}
``` | false | @Test
public void testToString() {
DoublePrecisionFloat doublePrecisionFloat = new DoublePrecisionFloat(1.234);
assertEquals("1.234", doublePrecisionFloat.toString());
} | 1,088 | src/test/java/co/nstant/in/cbor/model/DoublePrecisionFloatTest.java | @Override
public String toString() {
return String.valueOf(value);
} | 749 | src/main/java/co/nstant/in/cbor/model/DoublePrecisionFloat.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `getSimpleValueType` method in the `SimpleValue` class. Put new test methods inside the file `SimpleValueTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: SimpleValue.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class SimpleValue extends Special {
private final SimpleValueType simpleValueType;
public static final SimpleValue FALSE = new SimpleValue(SimpleValueType.FALSE);
public static final SimpleValue TRUE = new SimpleValue(SimpleValueType.TRUE);
public static final SimpleValue NULL = new SimpleValue(SimpleValueType.NULL);
public static final SimpleValue UNDEFINED = new SimpleValue(SimpleValueType.UNDEFINED);
private final int value;
public SimpleValue(SimpleValueType simpleValueType) {
super(SpecialType.SIMPLE_VALUE);
this.value = simpleValueType.getValue();
this.simpleValueType = simpleValueType;
}
public SimpleValue(int value) {
super(value <= 23 ? SpecialType.SIMPLE_VALUE : SpecialType.SIMPLE_VALUE_NEXT_BYTE);
this.value = value;
this.simpleValueType = SimpleValueType.ofByte(value);
}
public SimpleValueType getSimpleValueType() {
return simpleValueType;
}
public int getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return simpleValueType.toString();
}
}
```
File: SimpleValueType.java
Package: co.nstant.in.cbor.model
```
public enum SimpleValueType {
FALSE(20), TRUE(21), NULL(22), UNDEFINED(23), RESERVED(0), UNALLOCATED(0);
private SimpleValueType(int value) {
this.value = value;
}
public int getValue();
public static SimpleValueType ofByte(int b);
}
``` | SimpleValue | getSimpleValueType | SimpleValueTest.java | JUnit4 | File: SimpleValue.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class SimpleValue extends Special {
private final SimpleValueType simpleValueType;
public static final SimpleValue FALSE = new SimpleValue(SimpleValueType.FALSE);
public static final SimpleValue TRUE = new SimpleValue(SimpleValueType.TRUE);
public static final SimpleValue NULL = new SimpleValue(SimpleValueType.NULL);
public static final SimpleValue UNDEFINED = new SimpleValue(SimpleValueType.UNDEFINED);
private final int value;
public SimpleValue(SimpleValueType simpleValueType) {
super(SpecialType.SIMPLE_VALUE);
this.value = simpleValueType.getValue();
this.simpleValueType = simpleValueType;
}
public SimpleValue(int value) {
super(value <= 23 ? SpecialType.SIMPLE_VALUE : SpecialType.SIMPLE_VALUE_NEXT_BYTE);
this.value = value;
this.simpleValueType = SimpleValueType.ofByte(value);
}
public SimpleValueType getSimpleValueType() {
return simpleValueType;
}
public int getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return simpleValueType.toString();
}
}
```
File: SimpleValueType.java
Package: co.nstant.in.cbor.model
```
public enum SimpleValueType {
FALSE(20), TRUE(21), NULL(22), UNDEFINED(23), RESERVED(0), UNALLOCATED(0);
private SimpleValueType(int value) {
this.value = value;
}
public int getValue();
public static SimpleValueType ofByte(int b);
}
``` | false | @Test
public void shouldDecodeBoolean() throws CborException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
encoder.encode(SimpleValue.TRUE);
encoder.encode(SimpleValue.FALSE);
byte[] encodedBytes = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encodedBytes);
List<DataItem> dataItems = new CborDecoder(byteArrayInputStream).decode();
int result = 0;
int position = 1;
for (DataItem dataItem : dataItems) {
position++;
switch (dataItem.getMajorType()) {
case SPECIAL:
Special special = (Special) dataItem;
switch (special.getSpecialType()) {
case SIMPLE_VALUE:
SimpleValue simpleValue = (SimpleValue) special;
switch (simpleValue.getSimpleValueType()) {
case FALSE:
result += position * 2;
break;
case TRUE:
result += position * 3;
break;
default:
break;
}
break;
default:
break;
}
break;
default:
break;
}
}
assertEquals(12, result);
} | 490 | src/test/java/co/nstant/in/cbor/decoder/SimpleValueDecoderTest.java | public SimpleValueType getSimpleValueType() {
return simpleValueType;
} | 960 | src/main/java/co/nstant/in/cbor/model/SimpleValue.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `hashCode` method in the `Tag` class. Put new test methods inside the file `TagTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Tag.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class Tag extends DataItem {
private final long value;
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Tag other = (Tag) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return "Tag(" + value + ")";
}
}
``` | Tag | hashCode | TagTest.java | JUnit4 | File: Tag.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class Tag extends DataItem {
private final long value;
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Tag other = (Tag) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return "Tag(" + value + ")";
}
}
``` | false | @Test
public void testHashcode() {
DataItem superClass = new DataItem(MajorType.TAG);
for (long i = 0; i < 256; i++) {
Tag tag = new Tag(i);
assertEquals(tag.hashCode(), superClass.hashCode() ^ Objects.hashCode(i));
}
} | 246 | src/test/java/co/nstant/in/cbor/model/TagTest.java | @Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
} | 521 | src/main/java/co/nstant/in/cbor/model/Tag.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `equals` method in the `Tag` class. Put new test methods inside the file `TagTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Tag.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class Tag extends DataItem {
private final long value;
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Tag other = (Tag) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return "Tag(" + value + ")";
}
}
``` | Tag | equals | TagTest.java | JUnit4 | File: Tag.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class Tag extends DataItem {
private final long value;
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Tag other = (Tag) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return "Tag(" + value + ")";
}
}
``` | false | @Test
public void testEquals1() {
for (int i = 0; i < 256; i++) {
Tag tag1 = new Tag(i);
Tag tag2 = new Tag(i);
assertTrue(tag1.equals(tag2));
}
} | 527 | src/test/java/co/nstant/in/cbor/model/TagTest.java | @Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Tag other = (Tag) object;
return super.equals(object) && value == other.value;
}
return false;
} | 285 | src/main/java/co/nstant/in/cbor/model/Tag.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `equals` method in the `Tag` class. Put new test methods inside the file `TagTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Tag.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class Tag extends DataItem {
private final long value;
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Tag other = (Tag) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return "Tag(" + value + ")";
}
}
``` | Tag | equals | TagTest.java | JUnit4 | File: Tag.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class Tag extends DataItem {
private final long value;
public Tag(long value) {
super(MajorType.TAG);
this.value = value;
}
public long getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Tag other = (Tag) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return "Tag(" + value + ")";
}
}
``` | false | @Test
public void testEquals2() {
Tag tag = new Tag(0);
assertTrue(tag.equals(tag));
} | 739 | src/test/java/co/nstant/in/cbor/model/TagTest.java | @Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Tag other = (Tag) object;
return super.equals(object) && value == other.value;
}
return false;
} | 285 | src/main/java/co/nstant/in/cbor/model/Tag.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `ofByte` method in the `AdditionalInformation` class. Put new test methods inside the file `AdditionalInformationTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: AdditionalInformation.java
```
package co.nstant.in.cbor.model;
public enum AdditionalInformation {
DIRECT(0),
ONE_BYTE(24),
TWO_BYTES(25),
FOUR_BYTES(26),
EIGHT_BYTES(27),
RESERVED(28),
INDEFINITE(31);
private final int value;
private AdditionalInformation(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static AdditionalInformation ofByte(int b) {
switch (b & 31) {
case 24:
return ONE_BYTE;
case 25:
return TWO_BYTES;
case 26:
return FOUR_BYTES;
case 27:
return EIGHT_BYTES;
case 28:
case 29:
case 30:
return RESERVED;
case 31:
return INDEFINITE;
default:
return DIRECT;
}
}
}
```
File: AdditionalInformation.java
Package: co.nstant.in.cbor.model
```
/**
* The initial byte of each data item contains both information about the major
* type (the high-order 3 bits) and additional information (the low-order 5
* bits). When the value of the additional information is less than 24, it is
* directly used as a small unsigned integer. When it is 24 to 27, the
* additional bytes for a variable-length integer immediately follow; the values
* 24 to 27 of the additional information specify that its length is a 1-, 2-,
* 4- or 8-byte unsigned integer, respectively. Additional information value 31
* is used for indefinite length items, described in Section 2.2. Additional
* information values 28 to 30 are reserved for future expansion.
*/
public enum AdditionalInformation {
DIRECT(0), // 0-23
ONE_BYTE(24), // 24
TWO_BYTES(25), // 25
FOUR_BYTES(26), // 26
EIGHT_BYTES(27), // 27
RESERVED(28), // 28-30
INDEFINITE(31);
private AdditionalInformation(int value) {
this.value = value;
}
public int getValue();
public static AdditionalInformation ofByte(int b);
}
``` | AdditionalInformation | ofByte | AdditionalInformationTest.java | JUnit4 | File: AdditionalInformation.java
```
package co.nstant.in.cbor.model;
public enum AdditionalInformation {
DIRECT(0),
ONE_BYTE(24),
TWO_BYTES(25),
FOUR_BYTES(26),
EIGHT_BYTES(27),
RESERVED(28),
INDEFINITE(31);
private final int value;
private AdditionalInformation(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static AdditionalInformation ofByte(int b) {
switch (b & 31) {
case 24:
return ONE_BYTE;
case 25:
return TWO_BYTES;
case 26:
return FOUR_BYTES;
case 27:
return EIGHT_BYTES;
case 28:
case 29:
case 30:
return RESERVED;
case 31:
return INDEFINITE;
default:
return DIRECT;
}
}
}
```
File: AdditionalInformation.java
Package: co.nstant.in.cbor.model
```
/**
* The initial byte of each data item contains both information about the major
* type (the high-order 3 bits) and additional information (the low-order 5
* bits). When the value of the additional information is less than 24, it is
* directly used as a small unsigned integer. When it is 24 to 27, the
* additional bytes for a variable-length integer immediately follow; the values
* 24 to 27 of the additional information specify that its length is a 1-, 2-,
* 4- or 8-byte unsigned integer, respectively. Additional information value 31
* is used for indefinite length items, described in Section 2.2. Additional
* information values 28 to 30 are reserved for future expansion.
*/
public enum AdditionalInformation {
DIRECT(0), // 0-23
ONE_BYTE(24), // 24
TWO_BYTES(25), // 25
FOUR_BYTES(26), // 26
EIGHT_BYTES(27), // 27
RESERVED(28), // 28-30
INDEFINITE(31);
private AdditionalInformation(int value) {
this.value = value;
}
public int getValue();
public static AdditionalInformation ofByte(int b);
}
``` | false |
@Test
public void shouldHandleReserved28() {
Assert.assertEquals(AdditionalInformation.RESERVED, AdditionalInformation.ofByte(28));
Assert.assertEquals(AdditionalInformation.RESERVED, AdditionalInformation.ofByte(29));
Assert.assertEquals(AdditionalInformation.RESERVED, AdditionalInformation.ofByte(30));
} | 129 | src/test/java/co/nstant/in/cbor/model/AdditionalInformationTest.java | public static AdditionalInformation ofByte(int b) {
switch (b & 31) {
case 24:
return ONE_BYTE;
case 25:
return TWO_BYTES;
case 26:
return FOUR_BYTES;
case 27:
return EIGHT_BYTES;
case 28:
case 29:
case 30:
return RESERVED;
case 31:
return INDEFINITE;
default:
return DIRECT;
}
} | 1,119 | src/main/java/co/nstant/in/cbor/model/AdditionalInformation.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `add` method in the `CborBuilder` class. Put new test methods inside the file `CborBuilderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborBuilder.java
```
package co.nstant.in.cbor;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import co.nstant.in.cbor.builder.AbstractBuilder;
import co.nstant.in.cbor.builder.ArrayBuilder;
import co.nstant.in.cbor.builder.ByteStringBuilder;
import co.nstant.in.cbor.builder.MapBuilder;
import co.nstant.in.cbor.builder.UnicodeStringBuilder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.ByteString;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
import co.nstant.in.cbor.model.UnicodeString;
public class CborBuilder extends AbstractBuilder<CborBuilder> {
private final LinkedList<DataItem> dataItems = new LinkedList<>();
public CborBuilder() {
super(null);
}
public CborBuilder reset() {
dataItems.clear();
return this;
}
public List<DataItem> build() {
return dataItems;
}
public CborBuilder add(DataItem dataItem) {
dataItems.add(dataItem);
return this;
}
public CborBuilder add(long value) {
add(convert(value));
return this;
}
public CborBuilder add(BigInteger value) {
add(convert(value));
return this;
}
public CborBuilder add(boolean value) {
add(convert(value));
return this;
}
public CborBuilder add(float value) {
add(convert(value));
return this;
}
public CborBuilder add(double value) {
add(convert(value));
return this;
}
public CborBuilder add(byte[] bytes) {
add(convert(bytes));
return this;
}
public ByteStringBuilder<CborBuilder> startByteString() {
return startByteString(null);
}
public ByteStringBuilder<CborBuilder> startByteString(byte[] bytes) {
add(new ByteString(bytes).setChunked(true));
return new ByteStringBuilder<CborBuilder>(this);
}
public CborBuilder add(String string) {
add(convert(string));
return this;
}
public UnicodeStringBuilder<CborBuilder> startString() {
return startString(null);
}
public UnicodeStringBuilder<CborBuilder> startString(String string) {
add(new UnicodeString(string).setChunked(true));
return new UnicodeStringBuilder<CborBuilder>(this);
}
public CborBuilder addTag(long value) {
add(tag(value));
return this;
}
public CborBuilder tagged(long value) {
DataItem item = dataItems.peekLast();
if (item == null) {
throw new IllegalStateException("Can't add a tag before adding an item");
}
item.getOuterTaggable().setTag(value);
return this;
}
public ArrayBuilder<CborBuilder> startArray() {
Array array = new Array();
array.setChunked(true);
add(array);
return new ArrayBuilder<CborBuilder>(this, array);
}
public ArrayBuilder<CborBuilder> addArray() {
Array array = new Array();
add(array);
return new ArrayBuilder<CborBuilder>(this, array);
}
public MapBuilder<CborBuilder> addMap() {
Map map = new Map();
add(map);
return new MapBuilder<CborBuilder>(this, map);
}
public MapBuilder<CborBuilder> startMap() {
Map map = new Map();
map.setChunked(true);
add(map);
return new MapBuilder<CborBuilder>(this, map);
}
@Override
protected void addChunk(DataItem dataItem) {
add(dataItem);
}
}
```
File: CborBuilder.java
Package: co.nstant.in.cbor
```
public class CborBuilder extends AbstractBuilder<CborBuilder> {
public CborBuilder() {
super(null);
}
public CborBuilder reset();
public List<DataItem> build();
public CborBuilder add(DataItem dataItem);
public CborBuilder add(long value);
public CborBuilder add(BigInteger value);
public CborBuilder add(boolean value);
public CborBuilder add(float value);
public CborBuilder add(double value);
public CborBuilder add(byte[] bytes);
public ByteStringBuilder<CborBuilder> startByteString();
public ByteStringBuilder<CborBuilder> startByteString(byte[] bytes);
public CborBuilder add(String string);
public UnicodeStringBuilder<CborBuilder> startString();
public UnicodeStringBuilder<CborBuilder> startString(String string);
public CborBuilder addTag(long value);
public CborBuilder tagged(long value);
public ArrayBuilder<CborBuilder> startArray();
public ArrayBuilder<CborBuilder> addArray();
public MapBuilder<CborBuilder> addMap();
public MapBuilder<CborBuilder> startMap();
@Override
protected void addChunk(DataItem dataItem);
}
```
File: AbstractBuilder.java
Package: co.nstant.in.cbor.builder
```
public abstract class AbstractBuilder<T> {
public AbstractBuilder(T parent) {
this.parent = parent;
}
}
``` | CborBuilder | add | CborBuilderTest.java | JUnit4 | File: CborBuilder.java
```
package co.nstant.in.cbor;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import co.nstant.in.cbor.builder.AbstractBuilder;
import co.nstant.in.cbor.builder.ArrayBuilder;
import co.nstant.in.cbor.builder.ByteStringBuilder;
import co.nstant.in.cbor.builder.MapBuilder;
import co.nstant.in.cbor.builder.UnicodeStringBuilder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.ByteString;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
import co.nstant.in.cbor.model.UnicodeString;
public class CborBuilder extends AbstractBuilder<CborBuilder> {
private final LinkedList<DataItem> dataItems = new LinkedList<>();
public CborBuilder() {
super(null);
}
public CborBuilder reset() {
dataItems.clear();
return this;
}
public List<DataItem> build() {
return dataItems;
}
public CborBuilder add(DataItem dataItem) {
dataItems.add(dataItem);
return this;
}
public CborBuilder add(long value) {
add(convert(value));
return this;
}
public CborBuilder add(BigInteger value) {
add(convert(value));
return this;
}
public CborBuilder add(boolean value) {
add(convert(value));
return this;
}
public CborBuilder add(float value) {
add(convert(value));
return this;
}
public CborBuilder add(double value) {
add(convert(value));
return this;
}
public CborBuilder add(byte[] bytes) {
add(convert(bytes));
return this;
}
public ByteStringBuilder<CborBuilder> startByteString() {
return startByteString(null);
}
public ByteStringBuilder<CborBuilder> startByteString(byte[] bytes) {
add(new ByteString(bytes).setChunked(true));
return new ByteStringBuilder<CborBuilder>(this);
}
public CborBuilder add(String string) {
add(convert(string));
return this;
}
public UnicodeStringBuilder<CborBuilder> startString() {
return startString(null);
}
public UnicodeStringBuilder<CborBuilder> startString(String string) {
add(new UnicodeString(string).setChunked(true));
return new UnicodeStringBuilder<CborBuilder>(this);
}
public CborBuilder addTag(long value) {
add(tag(value));
return this;
}
public CborBuilder tagged(long value) {
DataItem item = dataItems.peekLast();
if (item == null) {
throw new IllegalStateException("Can't add a tag before adding an item");
}
item.getOuterTaggable().setTag(value);
return this;
}
public ArrayBuilder<CborBuilder> startArray() {
Array array = new Array();
array.setChunked(true);
add(array);
return new ArrayBuilder<CborBuilder>(this, array);
}
public ArrayBuilder<CborBuilder> addArray() {
Array array = new Array();
add(array);
return new ArrayBuilder<CborBuilder>(this, array);
}
public MapBuilder<CborBuilder> addMap() {
Map map = new Map();
add(map);
return new MapBuilder<CborBuilder>(this, map);
}
public MapBuilder<CborBuilder> startMap() {
Map map = new Map();
map.setChunked(true);
add(map);
return new MapBuilder<CborBuilder>(this, map);
}
@Override
protected void addChunk(DataItem dataItem) {
add(dataItem);
}
}
```
File: CborBuilder.java
Package: co.nstant.in.cbor
```
public class CborBuilder extends AbstractBuilder<CborBuilder> {
public CborBuilder() {
super(null);
}
public CborBuilder reset();
public List<DataItem> build();
public CborBuilder add(DataItem dataItem);
public CborBuilder add(long value);
public CborBuilder add(BigInteger value);
public CborBuilder add(boolean value);
public CborBuilder add(float value);
public CborBuilder add(double value);
public CborBuilder add(byte[] bytes);
public ByteStringBuilder<CborBuilder> startByteString();
public ByteStringBuilder<CborBuilder> startByteString(byte[] bytes);
public CborBuilder add(String string);
public UnicodeStringBuilder<CborBuilder> startString();
public UnicodeStringBuilder<CborBuilder> startString(String string);
public CborBuilder addTag(long value);
public CborBuilder tagged(long value);
public ArrayBuilder<CborBuilder> startArray();
public ArrayBuilder<CborBuilder> addArray();
public MapBuilder<CborBuilder> addMap();
public MapBuilder<CborBuilder> startMap();
@Override
protected void addChunk(DataItem dataItem);
}
```
File: AbstractBuilder.java
Package: co.nstant.in.cbor.builder
```
public abstract class AbstractBuilder<T> {
public AbstractBuilder(T parent) {
this.parent = parent;
}
}
``` | false | @Test
public void shouldResetDataItems() {
CborBuilder builder = new CborBuilder();
builder.add(true);
builder.add(1.0f);
assertEquals(2, builder.build().size());
builder.reset();
assertEquals(0, builder.build().size());
} | 279 | src/test/java/co/nstant/in/cbor/CborBuilderTest.java | public CborBuilder add(boolean value) {
add(convert(value));
return this;
} | 1,228 | src/main/java/co/nstant/in/cbor/CborBuilder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `build` method in the `CborBuilder` class. Put new test methods inside the file `CborBuilderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborBuilder.java
```
package co.nstant.in.cbor;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import co.nstant.in.cbor.builder.AbstractBuilder;
import co.nstant.in.cbor.builder.ArrayBuilder;
import co.nstant.in.cbor.builder.ByteStringBuilder;
import co.nstant.in.cbor.builder.MapBuilder;
import co.nstant.in.cbor.builder.UnicodeStringBuilder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.ByteString;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
import co.nstant.in.cbor.model.UnicodeString;
public class CborBuilder extends AbstractBuilder<CborBuilder> {
private final LinkedList<DataItem> dataItems = new LinkedList<>();
public CborBuilder() {
super(null);
}
public CborBuilder reset() {
dataItems.clear();
return this;
}
public List<DataItem> build() {
return dataItems;
}
public CborBuilder add(DataItem dataItem) {
dataItems.add(dataItem);
return this;
}
public CborBuilder add(long value) {
add(convert(value));
return this;
}
public CborBuilder add(BigInteger value) {
add(convert(value));
return this;
}
public CborBuilder add(boolean value) {
add(convert(value));
return this;
}
public CborBuilder add(float value) {
add(convert(value));
return this;
}
public CborBuilder add(double value) {
add(convert(value));
return this;
}
public CborBuilder add(byte[] bytes) {
add(convert(bytes));
return this;
}
public ByteStringBuilder<CborBuilder> startByteString() {
return startByteString(null);
}
public ByteStringBuilder<CborBuilder> startByteString(byte[] bytes) {
add(new ByteString(bytes).setChunked(true));
return new ByteStringBuilder<CborBuilder>(this);
}
public CborBuilder add(String string) {
add(convert(string));
return this;
}
public UnicodeStringBuilder<CborBuilder> startString() {
return startString(null);
}
public UnicodeStringBuilder<CborBuilder> startString(String string) {
add(new UnicodeString(string).setChunked(true));
return new UnicodeStringBuilder<CborBuilder>(this);
}
public CborBuilder addTag(long value) {
add(tag(value));
return this;
}
public CborBuilder tagged(long value) {
DataItem item = dataItems.peekLast();
if (item == null) {
throw new IllegalStateException("Can't add a tag before adding an item");
}
item.getOuterTaggable().setTag(value);
return this;
}
public ArrayBuilder<CborBuilder> startArray() {
Array array = new Array();
array.setChunked(true);
add(array);
return new ArrayBuilder<CborBuilder>(this, array);
}
public ArrayBuilder<CborBuilder> addArray() {
Array array = new Array();
add(array);
return new ArrayBuilder<CborBuilder>(this, array);
}
public MapBuilder<CborBuilder> addMap() {
Map map = new Map();
add(map);
return new MapBuilder<CborBuilder>(this, map);
}
public MapBuilder<CborBuilder> startMap() {
Map map = new Map();
map.setChunked(true);
add(map);
return new MapBuilder<CborBuilder>(this, map);
}
@Override
protected void addChunk(DataItem dataItem) {
add(dataItem);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | CborBuilder | build | CborBuilderTest.java | JUnit4 | File: CborBuilder.java
```
package co.nstant.in.cbor;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import co.nstant.in.cbor.builder.AbstractBuilder;
import co.nstant.in.cbor.builder.ArrayBuilder;
import co.nstant.in.cbor.builder.ByteStringBuilder;
import co.nstant.in.cbor.builder.MapBuilder;
import co.nstant.in.cbor.builder.UnicodeStringBuilder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.ByteString;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
import co.nstant.in.cbor.model.UnicodeString;
public class CborBuilder extends AbstractBuilder<CborBuilder> {
private final LinkedList<DataItem> dataItems = new LinkedList<>();
public CborBuilder() {
super(null);
}
public CborBuilder reset() {
dataItems.clear();
return this;
}
public List<DataItem> build() {
return dataItems;
}
public CborBuilder add(DataItem dataItem) {
dataItems.add(dataItem);
return this;
}
public CborBuilder add(long value) {
add(convert(value));
return this;
}
public CborBuilder add(BigInteger value) {
add(convert(value));
return this;
}
public CborBuilder add(boolean value) {
add(convert(value));
return this;
}
public CborBuilder add(float value) {
add(convert(value));
return this;
}
public CborBuilder add(double value) {
add(convert(value));
return this;
}
public CborBuilder add(byte[] bytes) {
add(convert(bytes));
return this;
}
public ByteStringBuilder<CborBuilder> startByteString() {
return startByteString(null);
}
public ByteStringBuilder<CborBuilder> startByteString(byte[] bytes) {
add(new ByteString(bytes).setChunked(true));
return new ByteStringBuilder<CborBuilder>(this);
}
public CborBuilder add(String string) {
add(convert(string));
return this;
}
public UnicodeStringBuilder<CborBuilder> startString() {
return startString(null);
}
public UnicodeStringBuilder<CborBuilder> startString(String string) {
add(new UnicodeString(string).setChunked(true));
return new UnicodeStringBuilder<CborBuilder>(this);
}
public CborBuilder addTag(long value) {
add(tag(value));
return this;
}
public CborBuilder tagged(long value) {
DataItem item = dataItems.peekLast();
if (item == null) {
throw new IllegalStateException("Can't add a tag before adding an item");
}
item.getOuterTaggable().setTag(value);
return this;
}
public ArrayBuilder<CborBuilder> startArray() {
Array array = new Array();
array.setChunked(true);
add(array);
return new ArrayBuilder<CborBuilder>(this, array);
}
public ArrayBuilder<CborBuilder> addArray() {
Array array = new Array();
add(array);
return new ArrayBuilder<CborBuilder>(this, array);
}
public MapBuilder<CborBuilder> addMap() {
Map map = new Map();
add(map);
return new MapBuilder<CborBuilder>(this, map);
}
public MapBuilder<CborBuilder> startMap() {
Map map = new Map();
map.setChunked(true);
add(map);
return new MapBuilder<CborBuilder>(this, map);
}
@Override
protected void addChunk(DataItem dataItem) {
add(dataItem);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void shouldAddTag() {
CborBuilder builder = new CborBuilder();
List<DataItem> dataItems = builder.addTag(1234).build();
assertEquals(1, dataItems.size());
assertTrue(dataItems.get(0) instanceof Tag);
assertEquals(1234, ((Tag) dataItems.get(0)).getValue());
} | 563 | src/test/java/co/nstant/in/cbor/CborBuilderTest.java | public List<DataItem> build() {
return dataItems;
} | 848 | src/main/java/co/nstant/in/cbor/CborBuilder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `toString` method in the `Special` class. Put new test methods inside the file `SpecialTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Special.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class Special extends DataItem {
public static final Special BREAK = new Special(SpecialType.BREAK);
private final SpecialType specialType;
protected Special(SpecialType specialType) {
super(MajorType.SPECIAL);
this.specialType = Objects.requireNonNull(specialType);
}
public SpecialType getSpecialType() {
return specialType;
}
@Override
public boolean equals(Object object) {
if (object instanceof Special) {
Special other = (Special) object;
return super.equals(object) && specialType == other.specialType;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(specialType);
}
@Override
public String toString() {
return specialType.name();
}
}
``` | Special | toString | SpecialTest.java | JUnit4 | File: Special.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class Special extends DataItem {
public static final Special BREAK = new Special(SpecialType.BREAK);
private final SpecialType specialType;
protected Special(SpecialType specialType) {
super(MajorType.SPECIAL);
this.specialType = Objects.requireNonNull(specialType);
}
public SpecialType getSpecialType() {
return specialType;
}
@Override
public boolean equals(Object object) {
if (object instanceof Special) {
Special other = (Special) object;
return super.equals(object) && specialType == other.specialType;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(specialType);
}
@Override
public String toString() {
return specialType.name();
}
}
``` | false | @Test
public void testToString() {
Special special = Special.BREAK;
assertEquals("BREAK", special.toString());
} | 136 | src/test/java/co/nstant/in/cbor/model/SpecialTest.java | @Override
public String toString() {
return specialType.name();
} | 828 | src/main/java/co/nstant/in/cbor/model/Special.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `add` method in the `Array` class. Put new test methods inside the file `ArrayTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Array.java
```
package co.nstant.in.cbor.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Array extends ChunkableDataItem {
private final ArrayList<DataItem> objects;
public Array() {
super(MajorType.ARRAY);
objects = new ArrayList<>();
}
public Array(int initialCapacity) {
super(MajorType.ARRAY);
objects = new ArrayList<>(initialCapacity);
}
public Array add(DataItem object) {
objects.add(object);
return this;
}
public List<DataItem> getDataItems() {
return objects;
}
@Override
public boolean equals(Object object) {
if (object instanceof Array) {
Array other = (Array) object;
return super.equals(object) && objects.equals(other.objects);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ objects.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder("[");
if (isChunked()) {
stringBuilder.append("_ ");
}
stringBuilder.append(Arrays.toString(objects.toArray()).substring(1));
return stringBuilder.toString();
}
public DataItem peekLast() {
if (objects.isEmpty()) {
return null;
}
return objects.get(objects.size() - 1);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
```
File: Array.java
Package: co.nstant.in.cbor.model
```
public class Array extends ChunkableDataItem {
public Array() {
super(MajorType.ARRAY);
objects = new ArrayList<>();
}
public Array(int initialCapacity) {
super(MajorType.ARRAY);
objects = new ArrayList<>(initialCapacity);
}
public Array add(DataItem object);
public List<DataItem> getDataItems();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
public DataItem peekLast();
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
``` | Array | add | ArrayTest.java | JUnit4 | File: Array.java
```
package co.nstant.in.cbor.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Array extends ChunkableDataItem {
private final ArrayList<DataItem> objects;
public Array() {
super(MajorType.ARRAY);
objects = new ArrayList<>();
}
public Array(int initialCapacity) {
super(MajorType.ARRAY);
objects = new ArrayList<>(initialCapacity);
}
public Array add(DataItem object) {
objects.add(object);
return this;
}
public List<DataItem> getDataItems() {
return objects;
}
@Override
public boolean equals(Object object) {
if (object instanceof Array) {
Array other = (Array) object;
return super.equals(object) && objects.equals(other.objects);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ objects.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder("[");
if (isChunked()) {
stringBuilder.append("_ ");
}
stringBuilder.append(Arrays.toString(objects.toArray()).substring(1));
return stringBuilder.toString();
}
public DataItem peekLast() {
if (objects.isEmpty()) {
return null;
}
return objects.get(objects.size() - 1);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
```
File: Array.java
Package: co.nstant.in.cbor.model
```
public class Array extends ChunkableDataItem {
public Array() {
super(MajorType.ARRAY);
objects = new ArrayList<>();
}
public Array(int initialCapacity) {
super(MajorType.ARRAY);
objects = new ArrayList<>(initialCapacity);
}
public Array add(DataItem object);
public List<DataItem> getDataItems();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
public DataItem peekLast();
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
``` | false | @Test
public void testHashcode() {
Array array1 = new Array().add(new UnicodeString("string"));
Array array2 = new Array().add(new UnicodeString("string"));
assertEquals(array1.hashCode(), array2.hashCode());
} | 339 | src/test/java/co/nstant/in/cbor/model/ArrayTest.java | public Array add(DataItem object) {
objects.add(object);
return this;
} | 439 | src/main/java/co/nstant/in/cbor/model/Array.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `add` method in the `Array` class. Put new test methods inside the file `ArrayTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Array.java
```
package co.nstant.in.cbor.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Array extends ChunkableDataItem {
private final ArrayList<DataItem> objects;
public Array() {
super(MajorType.ARRAY);
objects = new ArrayList<>();
}
public Array(int initialCapacity) {
super(MajorType.ARRAY);
objects = new ArrayList<>(initialCapacity);
}
public Array add(DataItem object) {
objects.add(object);
return this;
}
public List<DataItem> getDataItems() {
return objects;
}
@Override
public boolean equals(Object object) {
if (object instanceof Array) {
Array other = (Array) object;
return super.equals(object) && objects.equals(other.objects);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ objects.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder("[");
if (isChunked()) {
stringBuilder.append("_ ");
}
stringBuilder.append(Arrays.toString(objects.toArray()).substring(1));
return stringBuilder.toString();
}
public DataItem peekLast() {
if (objects.isEmpty()) {
return null;
}
return objects.get(objects.size() - 1);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
```
File: Array.java
Package: co.nstant.in.cbor.model
```
public class Array extends ChunkableDataItem {
public Array() {
super(MajorType.ARRAY);
objects = new ArrayList<>();
}
public Array(int initialCapacity) {
super(MajorType.ARRAY);
objects = new ArrayList<>(initialCapacity);
}
public Array add(DataItem object);
public List<DataItem> getDataItems();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
public DataItem peekLast();
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
``` | Array | add | ArrayTest.java | JUnit4 | File: Array.java
```
package co.nstant.in.cbor.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Array extends ChunkableDataItem {
private final ArrayList<DataItem> objects;
public Array() {
super(MajorType.ARRAY);
objects = new ArrayList<>();
}
public Array(int initialCapacity) {
super(MajorType.ARRAY);
objects = new ArrayList<>(initialCapacity);
}
public Array add(DataItem object) {
objects.add(object);
return this;
}
public List<DataItem> getDataItems() {
return objects;
}
@Override
public boolean equals(Object object) {
if (object instanceof Array) {
Array other = (Array) object;
return super.equals(object) && objects.equals(other.objects);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ objects.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder("[");
if (isChunked()) {
stringBuilder.append("_ ");
}
stringBuilder.append(Arrays.toString(objects.toArray()).substring(1));
return stringBuilder.toString();
}
public DataItem peekLast() {
if (objects.isEmpty()) {
return null;
}
return objects.get(objects.size() - 1);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
```
File: Array.java
Package: co.nstant.in.cbor.model
```
public class Array extends ChunkableDataItem {
public Array() {
super(MajorType.ARRAY);
objects = new ArrayList<>();
}
public Array(int initialCapacity) {
super(MajorType.ARRAY);
objects = new ArrayList<>(initialCapacity);
}
public Array add(DataItem object);
public List<DataItem> getDataItems();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
public DataItem peekLast();
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
``` | false | @Test
public void testToString() {
Array array = new Array().add(new UnicodeString("a"));
assertEquals("[a]", array.toString());
array.setChunked(true);
assertEquals("[_ a]", array.toString());
array.add(new UnicodeString("b"));
assertEquals("[_ a, b]", array.toString());
array.setChunked(false);
assertEquals("[a, b]", array.toString());
} | 587 | src/test/java/co/nstant/in/cbor/model/ArrayTest.java | public Array add(DataItem object) {
objects.add(object);
return this;
} | 439 | src/main/java/co/nstant/in/cbor/model/Array.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `decodeNext` method in the `CborDecoder` class. Put new test methods inside the file `CborDecoderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | CborDecoder | decodeNext | CborDecoderTest.java | JUnit4 | File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test(expected = CborException.class)
public void shouldThrowCborException2() throws CborException {
CborDecoder cborDecoder = new CborDecoder(new InputStream() {
@Override
public int read() throws IOException {
return (8 << 5);
}
});
cborDecoder.decodeNext();
} | 1,307 | src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java | /**
* Decodes exactly one DataItem from the input stream.
*
* @return a {@link DataItem} or null if end of stream has reached.
* @throws CborException if decoding failed
*/
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
} | 4,185 | src/main/java/co/nstant/in/cbor/CborDecoder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `isAutoDecodeInfinitiveMaps` method in the `CborDecoder` class. Put new test methods inside the file `CborDecoderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
``` | CborDecoder | isAutoDecodeInfinitiveMaps | CborDecoderTest.java | JUnit4 | File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
``` | false | @Test
public void shouldSetAutoDecodeInfinitiveMaps() {
InputStream inputStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
CborDecoder cborDecoder = new CborDecoder(inputStream);
assertTrue(cborDecoder.isAutoDecodeInfinitiveMaps());
cborDecoder.setAutoDecodeInfinitiveMaps(false);
assertFalse(cborDecoder.isAutoDecodeInfinitiveMaps());
} | 1,683 | src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java | public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
} | 8,698 | src/main/java/co/nstant/in/cbor/CborDecoder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `isAutoDecodeRationalNumbers` method in the `CborDecoder` class. Put new test methods inside the file `CborDecoderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
``` | CborDecoder | isAutoDecodeRationalNumbers | CborDecoderTest.java | JUnit4 | File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
``` | false | @Test
public void shouldSetAutoDecodeRationalNumbers() {
InputStream inputStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
CborDecoder cborDecoder = new CborDecoder(inputStream);
assertTrue(cborDecoder.isAutoDecodeRationalNumbers());
cborDecoder.setAutoDecodeRationalNumbers(false);
assertFalse(cborDecoder.isAutoDecodeRationalNumbers());
} | 2,083 | src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java | public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
} | 9,553 | src/main/java/co/nstant/in/cbor/CborDecoder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `isAutoDecodeLanguageTaggedStrings` method in the `CborDecoder` class. Put new test methods inside the file `CborDecoderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
``` | CborDecoder | isAutoDecodeLanguageTaggedStrings | CborDecoderTest.java | JUnit4 | File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
``` | false | @Test
public void shouldSetAutoDecodeLanguageTaggedStrings() {
InputStream inputStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
CborDecoder cborDecoder = new CborDecoder(inputStream);
assertTrue(cborDecoder.isAutoDecodeLanguageTaggedStrings());
cborDecoder.setAutoDecodeLanguageTaggedStrings(false);
assertFalse(cborDecoder.isAutoDecodeLanguageTaggedStrings());
} | 2,487 | src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java | public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
} | 9,810 | src/main/java/co/nstant/in/cbor/CborDecoder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `decode` method in the `CborDecoder` class. Put new test methods inside the file `CborDecoderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | CborDecoder | decode | CborDecoderTest.java | JUnit4 | File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void shouldDecodeTaggedTags() throws CborException {
DataItem decoded = CborDecoder.decode(new byte[] { (byte) 0xC1, (byte) 0xC2, 0x02 }).get(0);
Tag outer = new Tag(1);
Tag inner = new Tag(2);
UnsignedInteger expected = new UnsignedInteger(2);
inner.setTag(outer);
expected.setTag(inner);
assertEquals(expected, decoded);
} | 5,625 | src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java | /**
* Convenience method to decode a byte array directly.
*
* @param bytes the CBOR encoded data
* @return a list of {@link DataItem}s
* @throws CborException if decoding failed
*/
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
} | 2,804 | src/main/java/co/nstant/in/cbor/CborDecoder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `decodeNext` method in the `CborDecoder` class. Put new test methods inside the file `CborDecoderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | CborDecoder | decodeNext | CborDecoderTest.java | JUnit4 | File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void shouldDecodeTaggedRationalNumber() throws CborException {
List<DataItem> items = new CborBuilder().addTag(1).addTag(30).addArray().add(1).add(2).end().build();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CborEncoder encoder = new CborEncoder(baos);
encoder.encode(items);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
CborDecoder decoder = new CborDecoder(bais);
RationalNumber expected = new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(2));
expected.getTag().setTag(new Tag(1));
assertEquals(expected, decoder.decodeNext());
} | 6,034 | src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java | /**
* Decodes exactly one DataItem from the input stream.
*
* @return a {@link DataItem} or null if end of stream has reached.
* @throws CborException if decoding failed
*/
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
} | 4,185 | src/main/java/co/nstant/in/cbor/CborDecoder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `decode` method in the `CborDecoder` class. Put new test methods inside the file `CborDecoderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | CborDecoder | decode | CborDecoderTest.java | JUnit4 | File: CborDecoder.java
```
package co.nstant.in.cbor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import co.nstant.in.cbor.decoder.ArrayDecoder;
import co.nstant.in.cbor.decoder.ByteStringDecoder;
import co.nstant.in.cbor.decoder.MapDecoder;
import co.nstant.in.cbor.decoder.NegativeIntegerDecoder;
import co.nstant.in.cbor.decoder.SpecialDecoder;
import co.nstant.in.cbor.decoder.TagDecoder;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.decoder.UnsignedIntegerDecoder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.LanguageTaggedString;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.Number;
import co.nstant.in.cbor.model.RationalNumber;
import co.nstant.in.cbor.model.Tag;
import co.nstant.in.cbor.model.UnicodeString;
public class CborDecoder {
private final InputStream inputStream;
private final UnsignedIntegerDecoder unsignedIntegerDecoder;
private final NegativeIntegerDecoder negativeIntegerDecoder;
private final ByteStringDecoder byteStringDecoder;
private final UnicodeStringDecoder unicodeStringDecoder;
private final ArrayDecoder arrayDecoder;
private final MapDecoder mapDecoder;
private final TagDecoder tagDecoder;
private final SpecialDecoder specialDecoder;
private boolean autoDecodeInfinitiveArrays = true;
private boolean autoDecodeInfinitiveMaps = true;
private boolean autoDecodeInfinitiveByteStrings = true;
private boolean autoDecodeInfinitiveUnicodeStrings = true;
private boolean autoDecodeRationalNumbers = true;
private boolean autoDecodeLanguageTaggedStrings = true;
private boolean rejectDuplicateKeys = false;
public CborDecoder(InputStream inputStream) {
Objects.requireNonNull(inputStream);
this.inputStream = inputStream;
unsignedIntegerDecoder = new UnsignedIntegerDecoder(this, inputStream);
negativeIntegerDecoder = new NegativeIntegerDecoder(this, inputStream);
byteStringDecoder = new ByteStringDecoder(this, inputStream);
unicodeStringDecoder = new UnicodeStringDecoder(this, inputStream);
arrayDecoder = new ArrayDecoder(this, inputStream);
mapDecoder = new MapDecoder(this, inputStream);
tagDecoder = new TagDecoder(this, inputStream);
specialDecoder = new SpecialDecoder(this, inputStream);
}
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
}
public List<DataItem> decode() throws CborException {
List<DataItem> dataItems = new LinkedList<>();
DataItem dataItem;
while ((dataItem = decodeNext()) != null) {
dataItems.add(dataItem);
}
return dataItems;
}
public void decode(DataItemListener dataItemListener) throws CborException {
Objects.requireNonNull(dataItemListener);
DataItem dataItem = decodeNext();
while (dataItem != null) {
dataItemListener.onDataItem(dataItem);
dataItem = decodeNext();
}
}
public DataItem decodeNext() throws CborException {
int symbol;
try {
symbol = inputStream.read();
} catch (IOException ioException) {
throw new CborException(ioException);
}
if (symbol == -1) {
return null;
}
switch (MajorType.ofByte(symbol)) {
case ARRAY:
return arrayDecoder.decode(symbol);
case BYTE_STRING:
return byteStringDecoder.decode(symbol);
case MAP:
return mapDecoder.decode(symbol);
case NEGATIVE_INTEGER:
return negativeIntegerDecoder.decode(symbol);
case UNICODE_STRING:
return unicodeStringDecoder.decode(symbol);
case UNSIGNED_INTEGER:
return unsignedIntegerDecoder.decode(symbol);
case SPECIAL:
return specialDecoder.decode(symbol);
case TAG:
Tag tag = tagDecoder.decode(symbol);
DataItem next = decodeNext();
if (next == null) {
throw new CborException("Unexpected end of stream: tag without following data item.");
} else {
if (autoDecodeRationalNumbers && tag.getValue() == 30) {
return decodeRationalNumber(next);
} else if (autoDecodeLanguageTaggedStrings && tag.getValue() == 38) {
return decodeLanguageTaggedString(next);
} else {
DataItem itemToTag = next;
while (itemToTag.hasTag())
itemToTag = itemToTag.getTag();
itemToTag.setTag(tag);
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
}
private DataItem decodeLanguageTaggedString(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding LanguageTaggedString: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding LanguageTaggedString: array size is not 2");
}
DataItem languageDataItem = array.getDataItems().get(0);
if (!(languageDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: first data item is not an UnicodeString");
}
DataItem stringDataItem = array.getDataItems().get(1);
if (!(stringDataItem instanceof UnicodeString)) {
throw new CborException("Error decoding LanguageTaggedString: second data item is not an UnicodeString");
}
UnicodeString language = (UnicodeString) languageDataItem;
UnicodeString string = (UnicodeString) stringDataItem;
return new LanguageTaggedString(language, string);
}
private DataItem decodeRationalNumber(DataItem dataItem) throws CborException {
if (!(dataItem instanceof Array)) {
throw new CborException("Error decoding RationalNumber: not an array");
}
Array array = (Array) dataItem;
if (array.getDataItems().size() != 2) {
throw new CborException("Error decoding RationalNumber: array size is not 2");
}
DataItem numeratorDataItem = array.getDataItems().get(0);
if (!(numeratorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: first data item is not a number");
}
DataItem denominatorDataItem = array.getDataItems().get(1);
if (!(denominatorDataItem instanceof Number)) {
throw new CborException("Error decoding RationalNumber: second data item is not a number");
}
Number numerator = (Number) numeratorDataItem;
Number denominator = (Number) denominatorDataItem;
return new RationalNumber(numerator, denominator);
}
public boolean isAutoDecodeInfinitiveArrays() {
return autoDecodeInfinitiveArrays;
}
public void setAutoDecodeInfinitiveArrays(boolean autoDecodeInfinitiveArrays) {
this.autoDecodeInfinitiveArrays = autoDecodeInfinitiveArrays;
}
public boolean isAutoDecodeInfinitiveMaps() {
return autoDecodeInfinitiveMaps;
}
public void setAutoDecodeInfinitiveMaps(boolean autoDecodeInfinitiveMaps) {
this.autoDecodeInfinitiveMaps = autoDecodeInfinitiveMaps;
}
public boolean isAutoDecodeInfinitiveByteStrings() {
return autoDecodeInfinitiveByteStrings;
}
public void setAutoDecodeInfinitiveByteStrings(boolean autoDecodeInfinitiveByteStrings) {
this.autoDecodeInfinitiveByteStrings = autoDecodeInfinitiveByteStrings;
}
public boolean isAutoDecodeInfinitiveUnicodeStrings() {
return autoDecodeInfinitiveUnicodeStrings;
}
public void setAutoDecodeInfinitiveUnicodeStrings(boolean autoDecodeInfinitiveUnicodeStrings) {
this.autoDecodeInfinitiveUnicodeStrings = autoDecodeInfinitiveUnicodeStrings;
}
public boolean isAutoDecodeRationalNumbers() {
return autoDecodeRationalNumbers;
}
public void setAutoDecodeRationalNumbers(boolean autoDecodeRationalNumbers) {
this.autoDecodeRationalNumbers = autoDecodeRationalNumbers;
}
public boolean isAutoDecodeLanguageTaggedStrings() {
return autoDecodeLanguageTaggedStrings;
}
public void setAutoDecodeLanguageTaggedStrings(boolean autoDecodeLanguageTaggedStrings) {
this.autoDecodeLanguageTaggedStrings = autoDecodeLanguageTaggedStrings;
}
public boolean isRejectDuplicateKeys() {
return rejectDuplicateKeys;
}
public void setRejectDuplicateKeys(boolean rejectDuplicateKeys) {
this.rejectDuplicateKeys = rejectDuplicateKeys;
}
public void setMaxPreallocationSize(int maxSize) {
unsignedIntegerDecoder.setMaxPreallocationSize(maxSize);
negativeIntegerDecoder.setMaxPreallocationSize(maxSize);
byteStringDecoder.setMaxPreallocationSize(maxSize);
unicodeStringDecoder.setMaxPreallocationSize(maxSize);
arrayDecoder.setMaxPreallocationSize(maxSize);
mapDecoder.setMaxPreallocationSize(maxSize);
tagDecoder.setMaxPreallocationSize(maxSize);
specialDecoder.setMaxPreallocationSize(maxSize);
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void shouldThrowOnItemWithForgedLength() throws CborException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
AbstractEncoder<Long> maliciousEncoder = new AbstractEncoder<Long>(null, buffer) {
@Override
public void encode(Long length) throws CborException {
encodeTypeAndLength(MajorType.UNICODE_STRING, length.longValue());
}
};
maliciousEncoder.encode(Long.valueOf(Integer.MAX_VALUE + 1L));
byte[] maliciousString = buffer.toByteArray();
try {
CborDecoder.decode(maliciousString);
fail("Should have failed the huge allocation");
} catch (CborException e) {
assertThat("Exception message", e.getMessage(), containsString("limited to INTMAX"));
}
buffer.reset();
maliciousEncoder.encode(Long.valueOf(Integer.MAX_VALUE - 1));
maliciousString = buffer.toByteArray();
try {
CborDecoder.decode(maliciousString);
fail("Should have failed the huge allocation");
} catch (OutOfMemoryError e) {
}
CborDecoder decoder = new CborDecoder(new ByteArrayInputStream(maliciousString));
decoder.setMaxPreallocationSize(1024);
try {
decoder.decode();
fail("Should have failed with unexpected end of stream exception");
} catch (CborException e) {
}
} | 6,723 | src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java | /**
* Convenience method to decode a byte array directly.
*
* @param bytes the CBOR encoded data
* @return a list of {@link DataItem}s
* @throws CborException if decoding failed
*/
public static List<DataItem> decode(byte[] bytes) throws CborException {
return new CborDecoder(new ByteArrayInputStream(bytes)).decode();
} | 2,804 | src/main/java/co/nstant/in/cbor/CborDecoder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `equals` method in the `UnicodeString` class. Put new test methods inside the file `UnicodeStringTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: UnicodeString.java
```
package co.nstant.in.cbor.model;
public class UnicodeString extends ChunkableDataItem {
private final String string;
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString() {
if (string == null) {
return "null";
} else {
return string;
}
}
public String getString() {
return string;
}
@Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
}
@Override
public int hashCode() {
int hash = 0;
if (string != null) {
hash = super.hashCode();
hash += string.hashCode();
}
return hash;
}
}
``` | UnicodeString | equals | UnicodeStringTest.java | JUnit4 | File: UnicodeString.java
```
package co.nstant.in.cbor.model;
public class UnicodeString extends ChunkableDataItem {
private final String string;
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString() {
if (string == null) {
return "null";
} else {
return string;
}
}
public String getString() {
return string;
}
@Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
}
@Override
public int hashCode() {
int hash = 0;
if (string != null) {
hash = super.hashCode();
hash += string.hashCode();
}
return hash;
}
}
``` | false | @Test
public void shouldEqualsSameValue() {
UnicodeString unicodeString1 = new UnicodeString("string");
UnicodeString unicodeString2 = new UnicodeString("string");
Assert.assertTrue(unicodeString1.equals(unicodeString2));
Assert.assertTrue(unicodeString2.equals(unicodeString1));
} | 764 | src/test/java/co/nstant/in/cbor/model/UnicodeStringTest.java | @Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
} | 473 | src/main/java/co/nstant/in/cbor/model/UnicodeString.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `hashCode` method in the `UnicodeString` class. Put new test methods inside the file `UnicodeStringTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: UnicodeString.java
```
package co.nstant.in.cbor.model;
public class UnicodeString extends ChunkableDataItem {
private final String string;
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString() {
if (string == null) {
return "null";
} else {
return string;
}
}
public String getString() {
return string;
}
@Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
}
@Override
public int hashCode() {
int hash = 0;
if (string != null) {
hash = super.hashCode();
hash += string.hashCode();
}
return hash;
}
}
``` | UnicodeString | hashCode | UnicodeStringTest.java | JUnit4 | File: UnicodeString.java
```
package co.nstant.in.cbor.model;
public class UnicodeString extends ChunkableDataItem {
private final String string;
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString() {
if (string == null) {
return "null";
} else {
return string;
}
}
public String getString() {
return string;
}
@Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
}
@Override
public int hashCode() {
int hash = 0;
if (string != null) {
hash = super.hashCode();
hash += string.hashCode();
}
return hash;
}
}
``` | false | @Test
public void shouldHashNull() {
Assert.assertEquals(0, new UnicodeString(null).hashCode());
} | 1,091 | src/test/java/co/nstant/in/cbor/model/UnicodeStringTest.java | @Override
public int hashCode() {
int hash = 0;
if (string != null) {
hash = super.hashCode();
hash += string.hashCode();
}
return hash;
} | 864 | src/main/java/co/nstant/in/cbor/model/UnicodeString.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `equals` method in the `UnicodeString` class. Put new test methods inside the file `UnicodeStringTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: UnicodeString.java
```
package co.nstant.in.cbor.model;
public class UnicodeString extends ChunkableDataItem {
private final String string;
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString() {
if (string == null) {
return "null";
} else {
return string;
}
}
public String getString() {
return string;
}
@Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
}
@Override
public int hashCode() {
int hash = 0;
if (string != null) {
hash = super.hashCode();
hash += string.hashCode();
}
return hash;
}
}
``` | UnicodeString | equals | UnicodeStringTest.java | JUnit4 | File: UnicodeString.java
```
package co.nstant.in.cbor.model;
public class UnicodeString extends ChunkableDataItem {
private final String string;
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString() {
if (string == null) {
return "null";
} else {
return string;
}
}
public String getString() {
return string;
}
@Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
}
@Override
public int hashCode() {
int hash = 0;
if (string != null) {
hash = super.hashCode();
hash += string.hashCode();
}
return hash;
}
}
``` | false | @Test
public void shouldNotEqualOtherObjects() {
UnicodeString unicodeString = new UnicodeString("string");
Assert.assertFalse(unicodeString.equals(null));
Assert.assertFalse(unicodeString.equals(1));
Assert.assertFalse(unicodeString.equals("string"));
} | 1,211 | src/test/java/co/nstant/in/cbor/model/UnicodeStringTest.java | @Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
} | 473 | src/main/java/co/nstant/in/cbor/model/UnicodeString.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `equals` method in the `UnicodeString` class. Put new test methods inside the file `UnicodeStringTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: UnicodeString.java
```
package co.nstant.in.cbor.model;
public class UnicodeString extends ChunkableDataItem {
private final String string;
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString() {
if (string == null) {
return "null";
} else {
return string;
}
}
public String getString() {
return string;
}
@Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
}
@Override
public int hashCode() {
int hash = 0;
if (string != null) {
hash = super.hashCode();
hash += string.hashCode();
}
return hash;
}
}
``` | UnicodeString | equals | UnicodeStringTest.java | JUnit4 | File: UnicodeString.java
```
package co.nstant.in.cbor.model;
public class UnicodeString extends ChunkableDataItem {
private final String string;
public UnicodeString(String string) {
super(MajorType.UNICODE_STRING);
this.string = string;
}
@Override
public String toString() {
if (string == null) {
return "null";
} else {
return string;
}
}
public String getString() {
return string;
}
@Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
}
@Override
public int hashCode() {
int hash = 0;
if (string != null) {
hash = super.hashCode();
hash += string.hashCode();
}
return hash;
}
}
``` | false | @Test
public void shouldNotEquals() {
UnicodeString unicodeString1 = new UnicodeString(null);
UnicodeString unicodeString2 = new UnicodeString("");
Assert.assertFalse(unicodeString1.equals(unicodeString2));
} | 1,511 | src/test/java/co/nstant/in/cbor/model/UnicodeStringTest.java | @Override
public boolean equals(Object object) {
if (object instanceof UnicodeString && super.equals(object)) {
UnicodeString other = (UnicodeString) object;
if (string == null) {
return other.string == null;
} else {
return string.equals(other.string);
}
}
return false;
} | 473 | src/main/java/co/nstant/in/cbor/model/UnicodeString.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `getKeys` method in the `Map` class. Put new test methods inside the file `MapTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | Map | getKeys | MapTest.java | JUnit4 | File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void shouldUseLastOfDuplicateKeysByDefault() throws CborException {
byte[] bytes = new byte[] { (byte) 0xa2, 0x01, 0x01, 0x01, 0x02 };
List<DataItem> decoded = CborDecoder.decode(bytes);
Map map = (Map) decoded.get(0);
assertEquals(map.getKeys().size(), 1);
assertEquals(map.get(new UnsignedInteger(1)), new UnsignedInteger(2));
} | 1,078 | src/test/java/co/nstant/in/cbor/decoder/MapDecoderTest.java | public Collection<DataItem> getKeys() {
return keys;
} | 879 | src/main/java/co/nstant/in/cbor/model/Map.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `setChunked` method in the `ChunkableDataItem` class. Put new test methods inside the file `ChunkableDataItemTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: ChunkableDataItem.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
class ChunkableDataItem extends DataItem {
private boolean chunked = false;
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked() {
return chunked;
}
public ChunkableDataItem setChunked(boolean chunked) {
this.chunked = chunked;
return this;
}
@Override
public boolean equals(Object object) {
if (object instanceof ChunkableDataItem) {
ChunkableDataItem other = (ChunkableDataItem) object;
return super.equals(object) && chunked == other.chunked;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(chunked);
}
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | ChunkableDataItem | setChunked | ChunkableDataItemTest.java | JUnit4 | File: ChunkableDataItem.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
class ChunkableDataItem extends DataItem {
private boolean chunked = false;
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked() {
return chunked;
}
public ChunkableDataItem setChunked(boolean chunked) {
this.chunked = chunked;
return this;
}
@Override
public boolean equals(Object object) {
if (object instanceof ChunkableDataItem) {
ChunkableDataItem other = (ChunkableDataItem) object;
return super.equals(object) && chunked == other.chunked;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(chunked);
}
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void testEquals() {
TestDataItem item1 = new TestDataItem();
TestDataItem item2 = new TestDataItem();
assertEquals(item1, item2);
item1.setChunked(true);
item2.setChunked(false);
assertFalse(item1.equals(item2));
assertFalse(item1.equals(null));
} | 341 | src/test/java/co/nstant/in/cbor/model/ChunkableDataItemTest.java | public ChunkableDataItem setChunked(boolean chunked) {
this.chunked = chunked;
return this;
} | 299 | src/main/java/co/nstant/in/cbor/model/ChunkableDataItem.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `equals` method in the `AbstractFloat` class. Put new test methods inside the file `AbstractFloatTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: AbstractFloat.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class AbstractFloat extends Special {
private final float value;
public AbstractFloat(SpecialType specialType, float value) {
super(specialType);
this.value = value;
}
public float getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof AbstractFloat) {
AbstractFloat other = (AbstractFloat) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
}
``` | AbstractFloat | equals | AbstractFloatTest.java | JUnit4 | File: AbstractFloat.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class AbstractFloat extends Special {
private final float value;
public AbstractFloat(SpecialType specialType, float value) {
super(specialType);
this.value = value;
}
public float getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof AbstractFloat) {
AbstractFloat other = (AbstractFloat) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
}
``` | false | @Test
public void testEquals() {
AbstractFloat a = new AbstractFloat(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT, 0.0f);
AbstractFloat b = new AbstractFloat(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT, 0.3f);
assertEquals(a, a);
assertEquals(b, b);
assertFalse(a.equals(b));
assertFalse(a.equals(null));
assertFalse(a.equals("test"));
} | 213 | src/test/java/co/nstant/in/cbor/model/AbstractFloatTest.java | @Override
public boolean equals(Object object) {
if (object instanceof AbstractFloat) {
AbstractFloat other = (AbstractFloat) object;
return super.equals(object) && value == other.value;
}
return false;
} | 330 | src/main/java/co/nstant/in/cbor/model/AbstractFloat.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `hashCode` method in the `AbstractFloat` class. Put new test methods inside the file `AbstractFloatTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: AbstractFloat.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class AbstractFloat extends Special {
private final float value;
public AbstractFloat(SpecialType specialType, float value) {
super(specialType);
this.value = value;
}
public float getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof AbstractFloat) {
AbstractFloat other = (AbstractFloat) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
}
``` | AbstractFloat | hashCode | AbstractFloatTest.java | JUnit4 | File: AbstractFloat.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class AbstractFloat extends Special {
private final float value;
public AbstractFloat(SpecialType specialType, float value) {
super(specialType);
this.value = value;
}
public float getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof AbstractFloat) {
AbstractFloat other = (AbstractFloat) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
}
``` | false | @Test
public void testHashcode() {
Special superClass = new Special(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT);
AbstractFloat f = new AbstractFloat(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT, 0.0f);
assertEquals(superClass.hashCode() ^ Objects.hashCode(0.0f), f.hashCode());
} | 619 | src/test/java/co/nstant/in/cbor/model/AbstractFloatTest.java | @Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
} | 596 | src/main/java/co/nstant/in/cbor/model/AbstractFloat.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `getNumerator` method in the `RationalNumber` class. Put new test methods inside the file `RationalNumberTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: RationalNumber.java
```
package co.nstant.in.cbor.model;
import java.math.BigInteger;
import co.nstant.in.cbor.CborException;
public class RationalNumber extends Array {
public RationalNumber(Number numerator, Number denominator) throws CborException {
setTag(30);
if (numerator == null) {
throw new CborException("Numerator is null");
}
if (denominator == null) {
throw new CborException("Denominator is null");
}
if (denominator.getValue().equals(BigInteger.ZERO)) {
throw new CborException("Denominator is zero");
}
add(numerator);
add(denominator);
}
public Number getNumerator() {
return (Number) getDataItems().get(0);
}
public Number getDenominator() {
return (Number) getDataItems().get(1);
}
}
```
File: Number.java
Package: co.nstant.in.cbor.model
```
public abstract class Number extends DataItem {
protected Number(MajorType majorType, BigInteger value) {
super(majorType);
this.value = Objects.requireNonNull(value);
}
public BigInteger getValue();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | RationalNumber | getNumerator | RationalNumberTest.java | JUnit4 | File: RationalNumber.java
```
package co.nstant.in.cbor.model;
import java.math.BigInteger;
import co.nstant.in.cbor.CborException;
public class RationalNumber extends Array {
public RationalNumber(Number numerator, Number denominator) throws CborException {
setTag(30);
if (numerator == null) {
throw new CborException("Numerator is null");
}
if (denominator == null) {
throw new CborException("Denominator is null");
}
if (denominator.getValue().equals(BigInteger.ZERO)) {
throw new CborException("Denominator is zero");
}
add(numerator);
add(denominator);
}
public Number getNumerator() {
return (Number) getDataItems().get(0);
}
public Number getDenominator() {
return (Number) getDataItems().get(1);
}
}
```
File: Number.java
Package: co.nstant.in.cbor.model
```
public abstract class Number extends DataItem {
protected Number(MajorType majorType, BigInteger value) {
super(majorType);
this.value = Objects.requireNonNull(value);
}
public BigInteger getValue();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void shouldSetNumeratorAndDenominator() throws CborException {
UnsignedInteger one = new UnsignedInteger(1);
UnsignedInteger two = new UnsignedInteger(2);
RationalNumber rationalNumber = new RationalNumber(one, two);
assertEquals(one, rationalNumber.getNumerator());
assertEquals(two, rationalNumber.getDenominator());
} | 737 | src/test/java/co/nstant/in/cbor/model/RationalNumberTest.java | public Number getNumerator() {
return (Number) getDataItems().get(0);
} | 732 | src/main/java/co/nstant/in/cbor/model/RationalNumber.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `decode` method in the `SpecialDecoder` class. Put new test methods inside the file `SpecialDecoderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: SpecialDecoder.java
```
package co.nstant.in.cbor.decoder;
import java.io.InputStream;
import co.nstant.in.cbor.CborDecoder;
import co.nstant.in.cbor.CborException;
import co.nstant.in.cbor.model.SimpleValue;
import co.nstant.in.cbor.model.SimpleValueType;
import co.nstant.in.cbor.model.Special;
import co.nstant.in.cbor.model.SpecialType;
public class SpecialDecoder extends AbstractDecoder<Special> {
private final HalfPrecisionFloatDecoder halfPrecisionFloatDecoder;
private final SinglePrecisionFloatDecoder singlePrecisionFloatDecoder;
private final DoublePrecisionFloatDecoder doublePrecisionFloatDecoder;
public SpecialDecoder(CborDecoder decoder, InputStream inputStream) {
super(decoder, inputStream);
this.halfPrecisionFloatDecoder = new HalfPrecisionFloatDecoder(decoder, inputStream);
this.singlePrecisionFloatDecoder = new SinglePrecisionFloatDecoder(decoder, inputStream);
this.doublePrecisionFloatDecoder = new DoublePrecisionFloatDecoder(decoder, inputStream);
}
@Override
public Special decode(int initialByte) throws CborException {
switch (SpecialType.ofByte(initialByte)) {
case BREAK:
return Special.BREAK;
case SIMPLE_VALUE:
switch (SimpleValueType.ofByte(initialByte)) {
case FALSE:
return SimpleValue.FALSE;
case TRUE:
return SimpleValue.TRUE;
case NULL:
return SimpleValue.NULL;
case UNDEFINED:
return SimpleValue.UNDEFINED;
case UNALLOCATED:
return new SimpleValue(initialByte & 31);
case RESERVED:
default:
throw new CborException("Not implemented");
}
case IEEE_754_HALF_PRECISION_FLOAT:
return halfPrecisionFloatDecoder.decode(initialByte);
case IEEE_754_SINGLE_PRECISION_FLOAT:
return singlePrecisionFloatDecoder.decode(initialByte);
case IEEE_754_DOUBLE_PRECISION_FLOAT:
return doublePrecisionFloatDecoder.decode(initialByte);
case SIMPLE_VALUE_NEXT_BYTE:
return new SimpleValue(nextSymbol());
default:
throw new CborException("Not implemented");
}
}
}
```
File: Special.java
Package: co.nstant.in.cbor.model
```
public class Special extends DataItem {
public static final Special BREAK = new Special(SpecialType.BREAK);
protected Special(SpecialType specialType) {
super(MajorType.SPECIAL);
this.specialType = Objects.requireNonNull(specialType);
}
public SpecialType getSpecialType();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | SpecialDecoder | decode | SpecialDecoderTest.java | JUnit4 | File: SpecialDecoder.java
```
package co.nstant.in.cbor.decoder;
import java.io.InputStream;
import co.nstant.in.cbor.CborDecoder;
import co.nstant.in.cbor.CborException;
import co.nstant.in.cbor.model.SimpleValue;
import co.nstant.in.cbor.model.SimpleValueType;
import co.nstant.in.cbor.model.Special;
import co.nstant.in.cbor.model.SpecialType;
public class SpecialDecoder extends AbstractDecoder<Special> {
private final HalfPrecisionFloatDecoder halfPrecisionFloatDecoder;
private final SinglePrecisionFloatDecoder singlePrecisionFloatDecoder;
private final DoublePrecisionFloatDecoder doublePrecisionFloatDecoder;
public SpecialDecoder(CborDecoder decoder, InputStream inputStream) {
super(decoder, inputStream);
this.halfPrecisionFloatDecoder = new HalfPrecisionFloatDecoder(decoder, inputStream);
this.singlePrecisionFloatDecoder = new SinglePrecisionFloatDecoder(decoder, inputStream);
this.doublePrecisionFloatDecoder = new DoublePrecisionFloatDecoder(decoder, inputStream);
}
@Override
public Special decode(int initialByte) throws CborException {
switch (SpecialType.ofByte(initialByte)) {
case BREAK:
return Special.BREAK;
case SIMPLE_VALUE:
switch (SimpleValueType.ofByte(initialByte)) {
case FALSE:
return SimpleValue.FALSE;
case TRUE:
return SimpleValue.TRUE;
case NULL:
return SimpleValue.NULL;
case UNDEFINED:
return SimpleValue.UNDEFINED;
case UNALLOCATED:
return new SimpleValue(initialByte & 31);
case RESERVED:
default:
throw new CborException("Not implemented");
}
case IEEE_754_HALF_PRECISION_FLOAT:
return halfPrecisionFloatDecoder.decode(initialByte);
case IEEE_754_SINGLE_PRECISION_FLOAT:
return singlePrecisionFloatDecoder.decode(initialByte);
case IEEE_754_DOUBLE_PRECISION_FLOAT:
return doublePrecisionFloatDecoder.decode(initialByte);
case SIMPLE_VALUE_NEXT_BYTE:
return new SimpleValue(nextSymbol());
default:
throw new CborException("Not implemented");
}
}
}
```
File: Special.java
Package: co.nstant.in.cbor.model
```
public class Special extends DataItem {
public static final Special BREAK = new Special(SpecialType.BREAK);
protected Special(SpecialType specialType) {
super(MajorType.SPECIAL);
this.specialType = Objects.requireNonNull(specialType);
}
public SpecialType getSpecialType();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test(expected = CborException.class)
public void shouldThrowExceptionOnUnallocated() throws CborException {
SpecialDecoder decoder = new SpecialDecoder(null, null);
decoder.decode(28);
} | 140 | src/test/java/co/nstant/in/cbor/decoder/SpecialDecoderTest.java | @Override
public Special decode(int initialByte) throws CborException {
switch (SpecialType.ofByte(initialByte)) {
case BREAK:
return Special.BREAK;
case SIMPLE_VALUE:
switch (SimpleValueType.ofByte(initialByte)) {
case FALSE:
return SimpleValue.FALSE;
case TRUE:
return SimpleValue.TRUE;
case NULL:
return SimpleValue.NULL;
case UNDEFINED:
return SimpleValue.UNDEFINED;
case UNALLOCATED:
return new SimpleValue(initialByte & 31);
case RESERVED:
default:
throw new CborException("Not implemented");
}
case IEEE_754_HALF_PRECISION_FLOAT:
return halfPrecisionFloatDecoder.decode(initialByte);
case IEEE_754_SINGLE_PRECISION_FLOAT:
return singlePrecisionFloatDecoder.decode(initialByte);
case IEEE_754_DOUBLE_PRECISION_FLOAT:
return doublePrecisionFloatDecoder.decode(initialByte);
case SIMPLE_VALUE_NEXT_BYTE:
return new SimpleValue(nextSymbol());
default:
throw new CborException("Not implemented");
}
} | 1,018 | src/main/java/co/nstant/in/cbor/decoder/SpecialDecoder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `put` method in the `Map` class. Put new test methods inside the file `MapTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
```
File: Map.java
Package: co.nstant.in.cbor.model
```
public class Map extends ChunkableDataItem {
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value);
public DataItem get(DataItem key);
public DataItem remove(DataItem key);
public Collection<DataItem> getKeys();
public Collection<DataItem> getValues();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
``` | Map | put | MapTest.java | JUnit4 | File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
```
File: Map.java
Package: co.nstant.in.cbor.model
```
public class Map extends ChunkableDataItem {
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value);
public DataItem get(DataItem key);
public DataItem remove(DataItem key);
public Collection<DataItem> getKeys();
public Collection<DataItem> getValues();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
@Override
public String toString();
}
```
File: ChunkableDataItem.java
Package: co.nstant.in.cbor.model
```
class ChunkableDataItem extends DataItem {
protected ChunkableDataItem(MajorType majorType) {
super(majorType);
}
public boolean isChunked();
public ChunkableDataItem setChunked(boolean chunked);
@Override
public boolean equals(Object object);
@Override
public int hashCode();
}
``` | false | @Test
public void testRemove() {
UnicodeString key = new UnicodeString("key");
UnicodeString value = new UnicodeString("value");
Map map = new Map();
map.put(key, value);
assertEquals(1, map.getValues().size());
map.remove(key);
assertEquals(0, map.getValues().size());
} | 490 | src/test/java/co/nstant/in/cbor/model/MapTest.java | public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
} | 536 | src/main/java/co/nstant/in/cbor/model/Map.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `hashCode` method in the `Map` class. Put new test methods inside the file `MapTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
``` | Map | hashCode | MapTest.java | JUnit4 | File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
``` | false | @Test
public void testHashcode() {
Map map1 = new Map();
Map map2 = new Map();
assertEquals(map1.hashCode(), map2.hashCode());
map1.put(new UnicodeString("key"), new UnicodeString("value"));
assertNotEquals(map1.hashCode(), map2.hashCode());
} | 988 | src/test/java/co/nstant/in/cbor/model/MapTest.java | @Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
} | 1,270 | src/main/java/co/nstant/in/cbor/model/Map.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `toString` method in the `Map` class. Put new test methods inside the file `MapTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
``` | Map | toString | MapTest.java | JUnit4 | File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
``` | false | @Test
public void testToString() {
Map map = new Map();
assertEquals("{ }", map.toString());
map.put(new UnicodeString("key1"), new UnicodeString("value1"));
assertEquals("{ key1: value1 }", map.toString());
map.put(new UnicodeString("key2"), new UnicodeString("value2"));
assertEquals("{ key1: value1, key2: value2 }", map.toString());
map.setChunked(true);
assertEquals("{_ key1: value1, key2: value2 }", map.toString());
} | 1,285 | src/test/java/co/nstant/in/cbor/model/MapTest.java | @Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
} | 1,369 | src/main/java/co/nstant/in/cbor/model/Map.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `getValues` method in the `Map` class. Put new test methods inside the file `MapTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | Map | getValues | MapTest.java | JUnit4 | File: Map.java
```
package co.nstant.in.cbor.model;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class Map extends ChunkableDataItem {
private final LinkedHashMap<DataItem, DataItem> map;
private final List<DataItem> keys = new LinkedList<>();
public Map() {
super(MajorType.MAP);
map = new LinkedHashMap<>();
}
public Map(int initialCapacity) {
super(MajorType.MAP);
map = new LinkedHashMap<>(initialCapacity);
}
public Map put(DataItem key, DataItem value) {
if (map.put(key, value) == null) {
keys.add(key);
}
return this;
}
public DataItem get(DataItem key) {
return map.get(key);
}
public DataItem remove(DataItem key) {
keys.remove(key);
return map.remove(key);
}
public Collection<DataItem> getKeys() {
return keys;
}
public Collection<DataItem> getValues() {
return map.values();
}
@Override
public boolean equals(Object object) {
if (object instanceof Map) {
Map other = (Map) object;
return super.equals(object) && map.equals(other.map);
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ map.hashCode();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (isChunked()) {
stringBuilder.append("{_ ");
} else {
stringBuilder.append("{ ");
}
for (DataItem key : keys) {
stringBuilder.append(key).append(": ").append(map.get(key)).append(", ");
}
if (stringBuilder.toString().endsWith(", ")) {
stringBuilder.setLength(stringBuilder.length() - 2);
}
stringBuilder.append(" }");
return stringBuilder.toString();
}
}
```
File: DataItem.java
Package: co.nstant.in.cbor.model
```
public class DataItem {
protected DataItem(MajorType majorType) {
this.majorType = majorType;
Objects.requireNonNull(majorType, "majorType is null");
}
public MajorType getMajorType();
public void setTag(long tag);
public void setTag(Tag tag);
public void removeTag();
public Tag getTag();
public boolean hasTag();
@Override
public boolean equals(Object object);
@Override
public int hashCode();
public DataItem getOuterTaggable();
}
``` | false | @Test
public void testItemOrderIsPreserved() throws CborException {
List<DataItem> input = new CborBuilder().addMap().put(1, "v1").put(0, "v2").end().build();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
encoder.nonCanonical().encode(input);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
CborDecoder decoder = new CborDecoder(byteArrayInputStream);
List<DataItem> output = decoder.decode();
assertEquals(input, output);
DataItem dataItem = output.get(0);
assertEquals(MajorType.MAP, dataItem.getMajorType());
Map map = (Map) dataItem;
Collection<DataItem> values = map.getValues();
Iterator<DataItem> iterator = values.iterator();
assertEquals(new UnicodeString("v1"), iterator.next());
assertEquals(new UnicodeString("v2"), iterator.next());
} | 1,789 | src/test/java/co/nstant/in/cbor/model/MapTest.java | public Collection<DataItem> getValues() {
return map.values();
} | 951 | src/main/java/co/nstant/in/cbor/model/Map.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `ofByte` method in the `MajorType` class. Put new test methods inside the file `MajorTypeTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: MajorType.java
```
package co.nstant.in.cbor.model;
public enum MajorType {
INVALID(-1),
UNSIGNED_INTEGER(0),
NEGATIVE_INTEGER(1),
BYTE_STRING(2),
UNICODE_STRING(3),
ARRAY(4),
MAP(5),
TAG(6),
SPECIAL(7);
private final int value;
private MajorType(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static MajorType ofByte(int b) {
switch (b >> 5) {
case 0:
return UNSIGNED_INTEGER;
case 1:
return NEGATIVE_INTEGER;
case 2:
return BYTE_STRING;
case 3:
return UNICODE_STRING;
case 4:
return ARRAY;
case 5:
return MAP;
case 6:
return TAG;
case 7:
return SPECIAL;
default:
return INVALID;
}
}
}
```
File: MajorType.java
Package: co.nstant.in.cbor.model
```
public enum MajorType {
INVALID(-1),
UNSIGNED_INTEGER(0),
NEGATIVE_INTEGER(1),
BYTE_STRING(2),
UNICODE_STRING(3),
ARRAY(4),
MAP(5),
TAG(6),
SPECIAL(7);
private MajorType(int value) {
this.value = value;
}
public int getValue();
public static MajorType ofByte(int b);
}
``` | MajorType | ofByte | MajorTypeTest.java | JUnit4 | File: MajorType.java
```
package co.nstant.in.cbor.model;
public enum MajorType {
INVALID(-1),
UNSIGNED_INTEGER(0),
NEGATIVE_INTEGER(1),
BYTE_STRING(2),
UNICODE_STRING(3),
ARRAY(4),
MAP(5),
TAG(6),
SPECIAL(7);
private final int value;
private MajorType(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static MajorType ofByte(int b) {
switch (b >> 5) {
case 0:
return UNSIGNED_INTEGER;
case 1:
return NEGATIVE_INTEGER;
case 2:
return BYTE_STRING;
case 3:
return UNICODE_STRING;
case 4:
return ARRAY;
case 5:
return MAP;
case 6:
return TAG;
case 7:
return SPECIAL;
default:
return INVALID;
}
}
}
```
File: MajorType.java
Package: co.nstant.in.cbor.model
```
public enum MajorType {
INVALID(-1),
UNSIGNED_INTEGER(0),
NEGATIVE_INTEGER(1),
BYTE_STRING(2),
UNICODE_STRING(3),
ARRAY(4),
MAP(5),
TAG(6),
SPECIAL(7);
private MajorType(int value) {
this.value = value;
}
public int getValue();
public static MajorType ofByte(int b);
}
``` | false | @Test
public void shouldReturnInvalidOnInvalidByteValue() {
Assert.assertEquals(MajorType.INVALID, MajorType.ofByte(0xffffffff));
} | 1,231 | src/test/java/co/nstant/in/cbor/model/MajorTypeTest.java | public static MajorType ofByte(int b) {
switch (b >> 5) {
case 0:
return UNSIGNED_INTEGER;
case 1:
return NEGATIVE_INTEGER;
case 2:
return BYTE_STRING;
case 3:
return UNICODE_STRING;
case 4:
return ARRAY;
case 5:
return MAP;
case 6:
return TAG;
case 7:
return SPECIAL;
default:
return INVALID;
}
} | 4,472 | src/main/java/co/nstant/in/cbor/model/MajorType.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `hashCode` method in the `SimpleValue` class. Put new test methods inside the file `SimpleValueTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: SimpleValue.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class SimpleValue extends Special {
private final SimpleValueType simpleValueType;
public static final SimpleValue FALSE = new SimpleValue(SimpleValueType.FALSE);
public static final SimpleValue TRUE = new SimpleValue(SimpleValueType.TRUE);
public static final SimpleValue NULL = new SimpleValue(SimpleValueType.NULL);
public static final SimpleValue UNDEFINED = new SimpleValue(SimpleValueType.UNDEFINED);
private final int value;
public SimpleValue(SimpleValueType simpleValueType) {
super(SpecialType.SIMPLE_VALUE);
this.value = simpleValueType.getValue();
this.simpleValueType = simpleValueType;
}
public SimpleValue(int value) {
super(value <= 23 ? SpecialType.SIMPLE_VALUE : SpecialType.SIMPLE_VALUE_NEXT_BYTE);
this.value = value;
this.simpleValueType = SimpleValueType.ofByte(value);
}
public SimpleValueType getSimpleValueType() {
return simpleValueType;
}
public int getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return simpleValueType.toString();
}
}
``` | SimpleValue | hashCode | SimpleValueTest.java | JUnit4 | File: SimpleValue.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class SimpleValue extends Special {
private final SimpleValueType simpleValueType;
public static final SimpleValue FALSE = new SimpleValue(SimpleValueType.FALSE);
public static final SimpleValue TRUE = new SimpleValue(SimpleValueType.TRUE);
public static final SimpleValue NULL = new SimpleValue(SimpleValueType.NULL);
public static final SimpleValue UNDEFINED = new SimpleValue(SimpleValueType.UNDEFINED);
private final int value;
public SimpleValue(SimpleValueType simpleValueType) {
super(SpecialType.SIMPLE_VALUE);
this.value = simpleValueType.getValue();
this.simpleValueType = simpleValueType;
}
public SimpleValue(int value) {
super(value <= 23 ? SpecialType.SIMPLE_VALUE : SpecialType.SIMPLE_VALUE_NEXT_BYTE);
this.value = value;
this.simpleValueType = SimpleValueType.ofByte(value);
}
public SimpleValueType getSimpleValueType() {
return simpleValueType;
}
public int getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return simpleValueType.toString();
}
}
``` | false | @Test
public void testHashcode() {
Special superClass1 = new Special(SpecialType.SIMPLE_VALUE);
Special superClass2 = new Special(SpecialType.SIMPLE_VALUE_NEXT_BYTE);
for (int i = 1; i < 256; i++) {
SimpleValue simpleValue = new SimpleValue(i);
if (i <= 23) {
assertEquals(simpleValue.hashCode(), superClass1.hashCode() ^ Objects.hashCode(i));
} else {
assertEquals(simpleValue.hashCode(), superClass2.hashCode() ^ Objects.hashCode(i));
}
}
} | 254 | src/test/java/co/nstant/in/cbor/model/SimpleValueTest.java | @Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
} | 1,366 | src/main/java/co/nstant/in/cbor/model/SimpleValue.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `equals` method in the `SimpleValue` class. Put new test methods inside the file `SimpleValueTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: SimpleValue.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class SimpleValue extends Special {
private final SimpleValueType simpleValueType;
public static final SimpleValue FALSE = new SimpleValue(SimpleValueType.FALSE);
public static final SimpleValue TRUE = new SimpleValue(SimpleValueType.TRUE);
public static final SimpleValue NULL = new SimpleValue(SimpleValueType.NULL);
public static final SimpleValue UNDEFINED = new SimpleValue(SimpleValueType.UNDEFINED);
private final int value;
public SimpleValue(SimpleValueType simpleValueType) {
super(SpecialType.SIMPLE_VALUE);
this.value = simpleValueType.getValue();
this.simpleValueType = simpleValueType;
}
public SimpleValue(int value) {
super(value <= 23 ? SpecialType.SIMPLE_VALUE : SpecialType.SIMPLE_VALUE_NEXT_BYTE);
this.value = value;
this.simpleValueType = SimpleValueType.ofByte(value);
}
public SimpleValueType getSimpleValueType() {
return simpleValueType;
}
public int getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return simpleValueType.toString();
}
}
``` | SimpleValue | equals | SimpleValueTest.java | JUnit4 | File: SimpleValue.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class SimpleValue extends Special {
private final SimpleValueType simpleValueType;
public static final SimpleValue FALSE = new SimpleValue(SimpleValueType.FALSE);
public static final SimpleValue TRUE = new SimpleValue(SimpleValueType.TRUE);
public static final SimpleValue NULL = new SimpleValue(SimpleValueType.NULL);
public static final SimpleValue UNDEFINED = new SimpleValue(SimpleValueType.UNDEFINED);
private final int value;
public SimpleValue(SimpleValueType simpleValueType) {
super(SpecialType.SIMPLE_VALUE);
this.value = simpleValueType.getValue();
this.simpleValueType = simpleValueType;
}
public SimpleValue(int value) {
super(value <= 23 ? SpecialType.SIMPLE_VALUE : SpecialType.SIMPLE_VALUE_NEXT_BYTE);
this.value = value;
this.simpleValueType = SimpleValueType.ofByte(value);
}
public SimpleValueType getSimpleValueType() {
return simpleValueType;
}
public int getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return simpleValueType.toString();
}
}
``` | false | @Test
public void testEquals1() {
for (int i = 0; i < 256; i++) {
SimpleValue simpleValue1 = new SimpleValue(i);
SimpleValue simpleValue2 = new SimpleValue(i);
assertTrue(simpleValue1.equals(simpleValue2));
}
} | 822 | src/test/java/co/nstant/in/cbor/model/SimpleValueTest.java | @Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
} | 1,106 | src/main/java/co/nstant/in/cbor/model/SimpleValue.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `equals` method in the `SimpleValue` class. Put new test methods inside the file `SimpleValueTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: SimpleValue.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class SimpleValue extends Special {
private final SimpleValueType simpleValueType;
public static final SimpleValue FALSE = new SimpleValue(SimpleValueType.FALSE);
public static final SimpleValue TRUE = new SimpleValue(SimpleValueType.TRUE);
public static final SimpleValue NULL = new SimpleValue(SimpleValueType.NULL);
public static final SimpleValue UNDEFINED = new SimpleValue(SimpleValueType.UNDEFINED);
private final int value;
public SimpleValue(SimpleValueType simpleValueType) {
super(SpecialType.SIMPLE_VALUE);
this.value = simpleValueType.getValue();
this.simpleValueType = simpleValueType;
}
public SimpleValue(int value) {
super(value <= 23 ? SpecialType.SIMPLE_VALUE : SpecialType.SIMPLE_VALUE_NEXT_BYTE);
this.value = value;
this.simpleValueType = SimpleValueType.ofByte(value);
}
public SimpleValueType getSimpleValueType() {
return simpleValueType;
}
public int getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return simpleValueType.toString();
}
}
``` | SimpleValue | equals | SimpleValueTest.java | JUnit4 | File: SimpleValue.java
```
package co.nstant.in.cbor.model;
import java.util.Objects;
public class SimpleValue extends Special {
private final SimpleValueType simpleValueType;
public static final SimpleValue FALSE = new SimpleValue(SimpleValueType.FALSE);
public static final SimpleValue TRUE = new SimpleValue(SimpleValueType.TRUE);
public static final SimpleValue NULL = new SimpleValue(SimpleValueType.NULL);
public static final SimpleValue UNDEFINED = new SimpleValue(SimpleValueType.UNDEFINED);
private final int value;
public SimpleValue(SimpleValueType simpleValueType) {
super(SpecialType.SIMPLE_VALUE);
this.value = simpleValueType.getValue();
this.simpleValueType = simpleValueType;
}
public SimpleValue(int value) {
super(value <= 23 ? SpecialType.SIMPLE_VALUE : SpecialType.SIMPLE_VALUE_NEXT_BYTE);
this.value = value;
this.simpleValueType = SimpleValueType.ofByte(value);
}
public SimpleValueType getSimpleValueType() {
return simpleValueType;
}
public int getValue() {
return value;
}
@Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(value);
}
@Override
public String toString() {
return simpleValueType.toString();
}
}
``` | false | @Test
public void testEquals2() {
SimpleValue simpleValue = new SimpleValue(0);
assertFalse(simpleValue.equals(new SimpleValue(1)));
assertFalse(simpleValue.equals(null));
assertFalse(simpleValue.equals(false));
assertFalse(simpleValue.equals(""));
assertFalse(simpleValue.equals(1));
assertFalse(simpleValue.equals(1.1));
} | 1,098 | src/test/java/co/nstant/in/cbor/model/SimpleValueTest.java | @Override
public boolean equals(Object object) {
if (object instanceof SimpleValue) {
SimpleValue other = (SimpleValue) object;
return super.equals(object) && value == other.value;
}
return false;
} | 1,106 | src/main/java/co/nstant/in/cbor/model/SimpleValue.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `end` method in the `MapBuilder` class. Put new test methods inside the file `MapBuilderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: MapBuilder.java
```
package co.nstant.in.cbor.builder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
public class MapBuilder<T extends AbstractBuilder<?>> extends AbstractBuilder<T> {
private final Map map;
private DataItem lastItem = null;
public MapBuilder(T parent, Map map) {
super(parent);
this.map = map;
}
public MapBuilder<T> put(DataItem key, DataItem value) {
map.put(key, value);
lastItem = value;
return this;
}
public MapBuilder<T> put(long key, long value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, boolean value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, float value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, double value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, byte[] value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, String value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, long value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, boolean value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, float value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, double value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, byte[] value) {
map.put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, String value) {
put(convert(key), convert(value));
return this;
}
public ArrayBuilder<MapBuilder<T>> putArray(DataItem key) {
Array array = new Array();
put(key, array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> putArray(long key) {
Array array = new Array();
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> putArray(String key) {
Array array = new Array();
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> startArray(DataItem key) {
Array array = new Array();
array.setChunked(true);
put(key, array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> startArray(long key) {
return startArray(convert(key));
}
public ArrayBuilder<MapBuilder<T>> startArray(String key) {
Array array = new Array();
array.setChunked(true);
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public MapBuilder<MapBuilder<T>> putMap(DataItem key) {
Map nestedMap = new Map();
put(key, nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> putMap(long key) {
Map nestedMap = new Map();
put(convert(key), nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> putMap(String key) {
Map nestedMap = new Map();
put(convert(key), nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> startMap(DataItem key) {
return startMap(key, true);
}
private MapBuilder<MapBuilder<T>> startMap(DataItem key, boolean chunked) {
Map nestedMap = new Map();
nestedMap.setChunked(chunked);
put(key, nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(DataItem key) {
return startMap(key, false);
}
public MapBuilder<T> tagged(long tag) {
if (lastItem == null) {
throw new IllegalStateException("Can't add a tag before adding an item");
}
lastItem.getOuterTaggable().setTag(tag);
return this;
}
public MapBuilder<MapBuilder<T>> startMap(long key) {
return startMap(convert(key));
}
public MapBuilder<MapBuilder<T>> startMap(String key) {
return startMap(convert(key));
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(long key) {
return startMap(convert(key), false);
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(String key) {
return startMap(convert(key), false);
}
public T end() {
return getParent();
}
public MapEntryBuilder<MapBuilder<T>> addKey(long key) {
return new MapEntryBuilder<>(this, convert(key));
}
public MapEntryBuilder<MapBuilder<T>> addKey(String key) {
return new MapEntryBuilder<>(this, convert(key));
}
}
```
File:
Package: no package
```
T extends AbstractBuilder<?>
```
File: AbstractBuilder.java
Package: co.nstant.in.cbor.builder
```
public abstract class AbstractBuilder<T> {
public AbstractBuilder(T parent) {
this.parent = parent;
}
}
``` | MapBuilder | end | MapBuilderTest.java | JUnit4 | File: MapBuilder.java
```
package co.nstant.in.cbor.builder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
public class MapBuilder<T extends AbstractBuilder<?>> extends AbstractBuilder<T> {
private final Map map;
private DataItem lastItem = null;
public MapBuilder(T parent, Map map) {
super(parent);
this.map = map;
}
public MapBuilder<T> put(DataItem key, DataItem value) {
map.put(key, value);
lastItem = value;
return this;
}
public MapBuilder<T> put(long key, long value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, boolean value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, float value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, double value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, byte[] value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, String value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, long value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, boolean value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, float value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, double value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, byte[] value) {
map.put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, String value) {
put(convert(key), convert(value));
return this;
}
public ArrayBuilder<MapBuilder<T>> putArray(DataItem key) {
Array array = new Array();
put(key, array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> putArray(long key) {
Array array = new Array();
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> putArray(String key) {
Array array = new Array();
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> startArray(DataItem key) {
Array array = new Array();
array.setChunked(true);
put(key, array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> startArray(long key) {
return startArray(convert(key));
}
public ArrayBuilder<MapBuilder<T>> startArray(String key) {
Array array = new Array();
array.setChunked(true);
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public MapBuilder<MapBuilder<T>> putMap(DataItem key) {
Map nestedMap = new Map();
put(key, nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> putMap(long key) {
Map nestedMap = new Map();
put(convert(key), nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> putMap(String key) {
Map nestedMap = new Map();
put(convert(key), nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> startMap(DataItem key) {
return startMap(key, true);
}
private MapBuilder<MapBuilder<T>> startMap(DataItem key, boolean chunked) {
Map nestedMap = new Map();
nestedMap.setChunked(chunked);
put(key, nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(DataItem key) {
return startMap(key, false);
}
public MapBuilder<T> tagged(long tag) {
if (lastItem == null) {
throw new IllegalStateException("Can't add a tag before adding an item");
}
lastItem.getOuterTaggable().setTag(tag);
return this;
}
public MapBuilder<MapBuilder<T>> startMap(long key) {
return startMap(convert(key));
}
public MapBuilder<MapBuilder<T>> startMap(String key) {
return startMap(convert(key));
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(long key) {
return startMap(convert(key), false);
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(String key) {
return startMap(convert(key), false);
}
public T end() {
return getParent();
}
public MapEntryBuilder<MapBuilder<T>> addKey(long key) {
return new MapEntryBuilder<>(this, convert(key));
}
public MapEntryBuilder<MapBuilder<T>> addKey(String key) {
return new MapEntryBuilder<>(this, convert(key));
}
}
```
File:
Package: no package
```
T extends AbstractBuilder<?>
```
File: AbstractBuilder.java
Package: co.nstant.in.cbor.builder
```
public abstract class AbstractBuilder<T> {
public AbstractBuilder(T parent) {
this.parent = parent;
}
}
``` | false | @Test
public void testMapBuilder() {
List<DataItem> dataItems = new CborBuilder()
.addMap()
.put(new UnicodeString("key"), new UnicodeString("value"))
.put(1, true)
.put(2, "value".getBytes())
.put(3, 1.0d)
.put(4, 1.0f)
.put(5, 1L)
.put(6, "value")
.put("7", true)
.put("8", "value".getBytes())
.put("9", 1.0d)
.put("10", 1.0f)
.put("11", 1L)
.put("12", "value")
.putMap(13)
.end()
.putMap("14").end()
.putMap(new UnsignedInteger(15)).end()
.putArray(16).end()
.putArray("17").end()
.putArray(new UnsignedInteger(18)).end()
.addKey(19).value(true)
.addKey(20).value("value".getBytes())
.addKey(21).value(1.0d)
.addKey(22).value(1.0f)
.addKey(23).value(1L)
.addKey(24).value("value")
.addKey("25").value(true)
.addKey("26").value("value".getBytes())
.addKey("27").value(1.0d)
.addKey("28").value(1.0f)
.addKey("29").value(1L)
.addKey("30").value("value")
.end()
.startMap()
.startArray(1).end()
.startArray(new UnsignedInteger(2)).end()
.end()
.build();
assertEquals(2, dataItems.size());
assertTrue(dataItems.get(0) instanceof Map);
Map map = (Map) dataItems.get(0);
assertEquals(31, map.getKeys().size());
} | 461 | src/test/java/co/nstant/in/cbor/builder/MapBuilderTest.java | public T end() {
return getParent();
} | 4,882 | src/main/java/co/nstant/in/cbor/builder/MapBuilder.java | method |
||
https://github.com/c-rack/cbor-java.git | Apache License 2.0 | cabd70d02e864354117d73d96d70ab021d748188 | java | Write tests for the `end` method in the `MapBuilder` class. Put new test methods inside the file `MapBuilderTest.java`.
*Guideline:*
- Test file should be complete and compilable, without need for further actions.
- Write a description of the class and the method being tested.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parametrized tests.
- Use libraries available in the project: JUnit4.
*Sources:*
File: MapBuilder.java
```
package co.nstant.in.cbor.builder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
public class MapBuilder<T extends AbstractBuilder<?>> extends AbstractBuilder<T> {
private final Map map;
private DataItem lastItem = null;
public MapBuilder(T parent, Map map) {
super(parent);
this.map = map;
}
public MapBuilder<T> put(DataItem key, DataItem value) {
map.put(key, value);
lastItem = value;
return this;
}
public MapBuilder<T> put(long key, long value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, boolean value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, float value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, double value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, byte[] value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, String value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, long value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, boolean value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, float value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, double value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, byte[] value) {
map.put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, String value) {
put(convert(key), convert(value));
return this;
}
public ArrayBuilder<MapBuilder<T>> putArray(DataItem key) {
Array array = new Array();
put(key, array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> putArray(long key) {
Array array = new Array();
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> putArray(String key) {
Array array = new Array();
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> startArray(DataItem key) {
Array array = new Array();
array.setChunked(true);
put(key, array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> startArray(long key) {
return startArray(convert(key));
}
public ArrayBuilder<MapBuilder<T>> startArray(String key) {
Array array = new Array();
array.setChunked(true);
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public MapBuilder<MapBuilder<T>> putMap(DataItem key) {
Map nestedMap = new Map();
put(key, nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> putMap(long key) {
Map nestedMap = new Map();
put(convert(key), nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> putMap(String key) {
Map nestedMap = new Map();
put(convert(key), nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> startMap(DataItem key) {
return startMap(key, true);
}
private MapBuilder<MapBuilder<T>> startMap(DataItem key, boolean chunked) {
Map nestedMap = new Map();
nestedMap.setChunked(chunked);
put(key, nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(DataItem key) {
return startMap(key, false);
}
public MapBuilder<T> tagged(long tag) {
if (lastItem == null) {
throw new IllegalStateException("Can't add a tag before adding an item");
}
lastItem.getOuterTaggable().setTag(tag);
return this;
}
public MapBuilder<MapBuilder<T>> startMap(long key) {
return startMap(convert(key));
}
public MapBuilder<MapBuilder<T>> startMap(String key) {
return startMap(convert(key));
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(long key) {
return startMap(convert(key), false);
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(String key) {
return startMap(convert(key), false);
}
public T end() {
return getParent();
}
public MapEntryBuilder<MapBuilder<T>> addKey(long key) {
return new MapEntryBuilder<>(this, convert(key));
}
public MapEntryBuilder<MapBuilder<T>> addKey(String key) {
return new MapEntryBuilder<>(this, convert(key));
}
}
```
File:
Package: no package
```
T extends AbstractBuilder<?>
```
File: AbstractBuilder.java
Package: co.nstant.in.cbor.builder
```
public abstract class AbstractBuilder<T> {
public AbstractBuilder(T parent) {
this.parent = parent;
}
}
``` | MapBuilder | end | MapBuilderTest.java | JUnit4 | File: MapBuilder.java
```
package co.nstant.in.cbor.builder;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
public class MapBuilder<T extends AbstractBuilder<?>> extends AbstractBuilder<T> {
private final Map map;
private DataItem lastItem = null;
public MapBuilder(T parent, Map map) {
super(parent);
this.map = map;
}
public MapBuilder<T> put(DataItem key, DataItem value) {
map.put(key, value);
lastItem = value;
return this;
}
public MapBuilder<T> put(long key, long value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, boolean value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, float value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, double value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, byte[] value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(long key, String value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, long value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, boolean value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, float value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, double value) {
put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, byte[] value) {
map.put(convert(key), convert(value));
return this;
}
public MapBuilder<T> put(String key, String value) {
put(convert(key), convert(value));
return this;
}
public ArrayBuilder<MapBuilder<T>> putArray(DataItem key) {
Array array = new Array();
put(key, array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> putArray(long key) {
Array array = new Array();
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> putArray(String key) {
Array array = new Array();
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> startArray(DataItem key) {
Array array = new Array();
array.setChunked(true);
put(key, array);
return new ArrayBuilder<>(this, array);
}
public ArrayBuilder<MapBuilder<T>> startArray(long key) {
return startArray(convert(key));
}
public ArrayBuilder<MapBuilder<T>> startArray(String key) {
Array array = new Array();
array.setChunked(true);
put(convert(key), array);
return new ArrayBuilder<>(this, array);
}
public MapBuilder<MapBuilder<T>> putMap(DataItem key) {
Map nestedMap = new Map();
put(key, nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> putMap(long key) {
Map nestedMap = new Map();
put(convert(key), nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> putMap(String key) {
Map nestedMap = new Map();
put(convert(key), nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> startMap(DataItem key) {
return startMap(key, true);
}
private MapBuilder<MapBuilder<T>> startMap(DataItem key, boolean chunked) {
Map nestedMap = new Map();
nestedMap.setChunked(chunked);
put(key, nestedMap);
return new MapBuilder<>(this, nestedMap);
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(DataItem key) {
return startMap(key, false);
}
public MapBuilder<T> tagged(long tag) {
if (lastItem == null) {
throw new IllegalStateException("Can't add a tag before adding an item");
}
lastItem.getOuterTaggable().setTag(tag);
return this;
}
public MapBuilder<MapBuilder<T>> startMap(long key) {
return startMap(convert(key));
}
public MapBuilder<MapBuilder<T>> startMap(String key) {
return startMap(convert(key));
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(long key) {
return startMap(convert(key), false);
}
public MapBuilder<MapBuilder<T>> startMapNotChunked(String key) {
return startMap(convert(key), false);
}
public T end() {
return getParent();
}
public MapEntryBuilder<MapBuilder<T>> addKey(long key) {
return new MapEntryBuilder<>(this, convert(key));
}
public MapEntryBuilder<MapBuilder<T>> addKey(String key) {
return new MapEntryBuilder<>(this, convert(key));
}
}
```
File:
Package: no package
```
T extends AbstractBuilder<?>
```
File: AbstractBuilder.java
Package: co.nstant.in.cbor.builder
```
public abstract class AbstractBuilder<T> {
public AbstractBuilder(T parent) {
this.parent = parent;
}
}
``` | false | @Test
public void startMapInMap() {
CborBuilder builder = new CborBuilder();
List<DataItem> dataItems = builder.addMap().startMap(new ByteString(new byte[] { 0x01 })).put(1, 2).end()
.startMap(1).end().startMap("asdf").end().end().build();
Map rootMap = (Map) dataItems.get(0);
DataItem keys[] = new DataItem[3];
rootMap.getKeys().toArray(keys);
assertEquals(keys[0], new ByteString(new byte[] { 0x01 }));
assertEquals(keys[1], new UnsignedInteger(1));
assertEquals(keys[2], new UnicodeString("asdf"));
assertTrue(rootMap.get(keys[0]) instanceof Map);
assertTrue(rootMap.get(keys[1]) instanceof Map);
assertTrue(rootMap.get(keys[2]) instanceof Map);
} | 2,384 | src/test/java/co/nstant/in/cbor/builder/MapBuilderTest.java | public T end() {
return getParent();
} | 4,882 | src/main/java/co/nstant/in/cbor/builder/MapBuilder.java | method |