introduce protocol/version.go and remove big endian functions

This commit is contained in:
Lucas Clemente
2016-04-15 18:46:39 +02:00
parent 052822aea5
commit b6c9c9b6a4
8 changed files with 57 additions and 74 deletions

View File

@@ -0,0 +1,13 @@
package protocol_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestProtocol(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Protocol Suite")
}

15
protocol/version.go Normal file
View File

@@ -0,0 +1,15 @@
package protocol
// VersionNumber is a version number as int
type VersionNumber int
// VersionNumberToTag maps version numbers ('32') to tags ('Q032')
func VersionNumberToTag(vn VersionNumber) uint32 {
v := uint32(vn)
return 'Q' + ((v/100%10)+'0')<<8 + ((v/10%10)+'0')<<16 + ((v%10)+'0')<<24
}
// VersionTagToNumber is built from VersionNumberToTag in init()
func VersionTagToNumber(v uint32) VersionNumber {
return VersionNumber(((v>>8)&0xff-'0')*100 + ((v>>16)&0xff-'0')*10 + ((v>>24)&0xff - '0'))
}

18
protocol/version_test.go Normal file
View File

@@ -0,0 +1,18 @@
package protocol_test
import (
"github.com/lucas-clemente/quic-go/protocol"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Version", func() {
It("converts tags to numbers", func() {
Expect(protocol.VersionTagToNumber('Q' + '1'<<8 + '2'<<16 + '3'<<24)).To(Equal(protocol.VersionNumber(123)))
})
It("converts number to tag", func() {
Expect(protocol.VersionNumberToTag(protocol.VersionNumber(123))).To(Equal(uint32('Q' + '1'<<8 + '2'<<16 + '3'<<24)))
})
})