Push latest work
This commit is contained in:
parent
852ddf8972
commit
a9abed7279
2 changed files with 42 additions and 20 deletions
|
@ -13,16 +13,31 @@ class UUID {
|
|||
LSB = lsb.abs();
|
||||
}
|
||||
|
||||
UUID.fromBytes(List<int> bytes) {
|
||||
int msb = 0;
|
||||
int lsb = 0;
|
||||
|
||||
int i = 0;
|
||||
for (i = 0; i < 8; ++i) {
|
||||
msb = msb << 8 | bytes[i] & 255;
|
||||
}
|
||||
|
||||
for (i = 8; i < 16; ++i) {
|
||||
lsb = lsb << 8 | bytes[i] & 255;
|
||||
}
|
||||
|
||||
this.MSB = msb;
|
||||
this.LSB = lsb;
|
||||
}
|
||||
|
||||
static final UUID ZERO = UUID.generate(0);
|
||||
|
||||
/// Validates whether the given [uuid] is a valid UUID.
|
||||
static bool validate(String uuid) {
|
||||
try {
|
||||
parse(uuid);
|
||||
return true;
|
||||
} catch (e) {
|
||||
if (uuid.length == ((16 * 2) + 4)) {
|
||||
return true; // Likely is true. This is just a surface level check
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the given [uuid] string and returns a UUID object.
|
||||
|
@ -114,21 +129,12 @@ class UUID {
|
|||
var msb = layer.readUnsignedLong();
|
||||
var lsb = layer.readUnsignedLong();
|
||||
|
||||
layer.clear();
|
||||
layer.writeUnsignedLong(lsb);
|
||||
layer.resetPosition();
|
||||
|
||||
csv = "";
|
||||
for (int byte in layer.readBytes(8)) {
|
||||
csv += "0x${byte.toRadixString(16)}, ";
|
||||
}
|
||||
|
||||
print("BYTES CSV: ${csv}");
|
||||
|
||||
if (msb < 0) print("Most significant bit is negative! ${msb}");
|
||||
if (lsb < 0) print("Least significant bit is negative! ${lsb}");
|
||||
|
||||
print("Forming UUID using MSB-LSB - ${msb} - ${lsb}");
|
||||
layer.resetPosition();
|
||||
return UUID.fromBytes(layer.readBytes(16));
|
||||
return UUID(msb, lsb);
|
||||
}
|
||||
case 4:
|
||||
|
|
|
@ -3,6 +3,11 @@ import 'package:libac_flutter/nbt/Stream.dart';
|
|||
import 'package:libac_flutter/utils/uuid/UUID.dart';
|
||||
|
||||
void main() {
|
||||
test("Store a 64bit value in int", () {
|
||||
int X = 9223372036854775807;
|
||||
expect(X.toString(), "9223372036854775807");
|
||||
});
|
||||
|
||||
test("Generate a UUID v4", () {
|
||||
var ID = UUID.generate(4);
|
||||
expect(UUID.validate(ID.toString()), true);
|
||||
|
@ -22,8 +27,8 @@ void main() {
|
|||
test("Check UUIDv4 for validity", () {
|
||||
var ID = UUID.generate(4);
|
||||
ByteLayer layer = ByteLayer();
|
||||
layer.writeLong(ID.getMostSignificantBits());
|
||||
layer.writeLong(ID.getLeastSignificantBits());
|
||||
layer.writeLong(ID.getMostSignificantBits().toInt());
|
||||
layer.writeLong(ID.getLeastSignificantBits().toInt());
|
||||
|
||||
print(
|
||||
"Checking version bit: ${layer.checkBit(6, 0x40)} - ${layer.getBit(6)}");
|
||||
|
@ -33,8 +38,8 @@ void main() {
|
|||
test("Generate and check a UUIDv3", () {
|
||||
var ID3 = UUID.generate(3, parameters: ["Test", "Test2"]);
|
||||
ByteLayer layer = ByteLayer();
|
||||
layer.writeLong(ID3.getMostSignificantBits());
|
||||
layer.writeLong(ID3.getLeastSignificantBits());
|
||||
layer.writeLong(ID3.getMostSignificantBits().toInt());
|
||||
layer.writeLong(ID3.getLeastSignificantBits().toInt());
|
||||
|
||||
print(
|
||||
"Checking version bit: ${layer.checkBit(6, 0x30)} - ${layer.getBit(6)}");
|
||||
|
@ -59,4 +64,15 @@ void main() {
|
|||
|
||||
expect(ID3.toString(), expected);
|
||||
});
|
||||
|
||||
test("Parse a v3 and compare", () {
|
||||
var asString = "3e1b8c8a-efab-381b-ab57-4764c45b0889";
|
||||
var ID3 = UUID.parse(asString);
|
||||
var ID3X = UUID.generate(3, parameters: ["OfflinePlayer:zontreck", ""]);
|
||||
|
||||
expect(ID3.MSB, ID3X.MSB);
|
||||
expect(ID3.LSB, ID3X.LSB);
|
||||
|
||||
expect(ID3.toString(), asString);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue