initial commits
parent
429fa92d5b
commit
997a5dc37a
|
@ -0,0 +1,5 @@
|
|||
package iid
|
||||
|
||||
const (
|
||||
badvalue = 0b0000000000000000000000000000000000000000000000000000000000000000
|
||||
)
|
|
@ -0,0 +1,62 @@
|
|||
package iid
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
var (
|
||||
base64encoding = base64.NewEncoding(
|
||||
"-"+
|
||||
"0123456789"+
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
|
||||
"_"+
|
||||
"abcdefghijklmnopqrstuvwxyz",
|
||||
).WithPadding(base64.NoPadding)
|
||||
)
|
||||
|
||||
func serialize(value uint64) string {
|
||||
|
||||
var storage bytes.Buffer
|
||||
{
|
||||
|
||||
err := binary.Write(&storage, binary.BigEndian, value)
|
||||
if nil != err {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
var encoded string
|
||||
{
|
||||
encoded = base64encoding.EncodeToString(storage.Bytes())
|
||||
}
|
||||
|
||||
return encoded
|
||||
}
|
||||
|
||||
func unserialize(value string) (uint64, bool) {
|
||||
|
||||
var p []byte
|
||||
{
|
||||
var err error
|
||||
|
||||
p, err = base64encoding.DecodeString(value)
|
||||
if nil != err {
|
||||
return badvalue, false
|
||||
}
|
||||
}
|
||||
|
||||
var result uint64
|
||||
{
|
||||
var reader io.Reader = bytes.NewReader(p)
|
||||
|
||||
err := binary.Read(reader, binary.BigEndian, &result)
|
||||
if nil != err {
|
||||
return badvalue, false
|
||||
}
|
||||
}
|
||||
|
||||
return result, true
|
||||
}
|
|
@ -0,0 +1,487 @@
|
|||
package iid
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSerialize(t *testing.T) {
|
||||
|
||||
tests := []struct{
|
||||
Value uint64
|
||||
Expected string
|
||||
}{
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000000000001,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000000000010,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000000000100,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000000001000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000000010000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000000100000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000001000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000010000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000000100000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000001000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000010000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000000100000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000001000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000010000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000000100000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000001000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000010000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000000100000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000001000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000010000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000000100000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000001000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000010000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000100000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000001000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000010000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000100000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000001000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000010000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000001000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000010000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000100000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000001000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000010000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000000100000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000001000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000010000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000000100000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000001000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000010000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000000100000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000001000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000010000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000000100000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000001000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000010000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000000100000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000001000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000010000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000000100000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000001000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000010000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000000100000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000001000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000010000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000100000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000001000000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000010000000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0000100000000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0001000000000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0010000000000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
{
|
||||
Value: 0b0100000000000000000000000000000000000000000000000000000000000000,
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111111111110,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111111111101,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111111111011,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111111110111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111111101111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111111011111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111110111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111101111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111011111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111110111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111101111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111011111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111110111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111101111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111011111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111110111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111101111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111011111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111110111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111101111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111011111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111110111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111101111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111011111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111110111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111101111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111011111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111110111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111101111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111011111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111110111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111101111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111011111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111110111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111101111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111011111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111110111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111101111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111011111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111110111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111101111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111011111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111110111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111101111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111011111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111110111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111101111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111011111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111110111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111101111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111011111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111110111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111101111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111011111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111110111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111101111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111011111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111110111111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111101111111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111011111111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0110111111111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0101111111111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0011111111111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111111111111111111111111111,
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000111111111111111111111111,
|
||||
},
|
||||
{
|
||||
Value: 0b0111111111111111111111111111111111111111000000000000000000000000,
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
Value: 0b0000000000000000000000000000000000000000100000000000000000000001,
|
||||
},
|
||||
{
|
||||
Value: 0b0100000000000000000000000000000000000001000000000000000000000000,
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
{
|
||||
Value: 0b0000000001100001100001110110100111011001110000111111000101100100,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000001100001100001110110101000001110110110001111101101100001,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000001100001100001110110101000100000100100010111101000001110,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000001100001100001110110101000111010100110110011110001010101,
|
||||
},
|
||||
{
|
||||
Value: 0b0000000001100001100001110110101001010010100011000011110001000100,
|
||||
},
|
||||
}
|
||||
|
||||
for testNumber, test := range tests {
|
||||
|
||||
var serialized string = serialize(test.Value)
|
||||
|
||||
actual, successful := unserialize(serialized)
|
||||
if !successful {
|
||||
t.Errorf("For test #%d, expected unserialization of serialized data to be successful but wasn't." , testNumber)
|
||||
t.Logf("SUCCESSFUL: %t", successful)
|
||||
t.Logf("VALUE: %064b", test.Value)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if expected := test.Value; expected != actual {
|
||||
t.Errorf("For test #%d, ", testNumber)
|
||||
t.Logf("SUCCESSFUL: %t", successful)
|
||||
t.Logf("EXPECTED: %064b", expected)
|
||||
t.Logf("ACTUAL: %064b", actual)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue