Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Jul 5, 2024
0 parents commit 4ef8856
Show file tree
Hide file tree
Showing 13 changed files with 1,124 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Go

on:
push:
branches: ["master"]
pull_request:
branches: ["master"]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "stable"

- name: Build
run: go build -v ./...

build-readme:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "stable"

- name: Build go snippets in readme
run: |
mkdir -p ~/.local/bin/
wget -O ~/.local/bin/lintdown.sh https://raw.githubusercontent.com/ChillerDragon/lintdown.sh/master/lintdown.sh
chmod +x ~/.local/bin/lintdown.sh
lintdown.sh README.md
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "stable"

- name: Test
run: go test -v -race -count=1 ./...

format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "stable"

- name: Format
run: diff -u <(echo -n) <(gofmt -d ./)
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 John Behm

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# varint

varint is a simple variable-length integer encoding. It is a way to store integers in a space-efficient manner.
This variant of varint is space efficient for small integers and is used in the Teeworlds network protocol.

Additionally this linrary also provides functions that operate on 64bit integers which is out of scope of the Teeeworlds protocol.
These varants may be used for security research or other purposes.

```text
/ Format: ESDDDDDD EDDDDDDD EDD... Extended, Sign, Data,
// E: is next byte part of the current integer
// S: Sign of integer 0 = positive, 1 = negative
// Data, Integer bits that follow the sign
```

## Example

```go
package main

import (
"fmt"

"github.com/teeworlds-go/varint"
)

func main() {
buf := make([]byte, varint.MaxVarintLen32)
written := varint.PutVarint(buf, 33)
out, read := varint.Varint(buf)
fmt.Println("written:", written)
fmt.Println("read:", read)
fmt.Println("value:", out)
// Output:
// written: 1
// read: 1
// value: 33
}
```

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/teeworlds-go/varint

go 1.22.5
108 changes: 108 additions & 0 deletions internal/testutils/require/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package require

import (
"cmp"
"fmt"
"reflect"
"testing"
)

func GreaterOrEqual(t *testing.T, expected, actual any, msgAndArgs ...any) {
t.Helper()

if compare(t, actual, expected) < 0 {
FailNow(t, fmt.Sprintf("expected actual value: %v to be greater or equal to: %v", actual, expected), msgAndArgs...)
}
}

func Greater(t *testing.T, expected, actual any, msgAndArgs ...any) {
t.Helper()

if compare(t, actual, expected) <= 0 {
FailNow(t, fmt.Sprintf("expected actual value: %v to be greater than: %v", actual, expected), msgAndArgs...)
}
}

func LessOrEqual(t *testing.T, expected, actual any, msgAndArgs ...any) {
t.Helper()

if compare(t, actual, expected) > 0 {
FailNow(t, fmt.Sprintf("expected actual value: %v to be less or equal to: %v", actual, expected), msgAndArgs...)
}
}

func Less(t *testing.T, expected, actual any, msgAndArgs ...any) {
t.Helper()

if compare(t, actual, expected) >= 0 {
FailNow(t, fmt.Sprintf("expected actual value: %v to be less than: %v", actual, expected), msgAndArgs...)
}
}

// compare returns
//
// -1 if expected is less than actual,
//
// 0 if expected equals actual,
//
// +1 if expected is greater than actual.
func compare(t *testing.T, expected, actual any) int {
t.Helper()

e := reflect.ValueOf(expected)
a := reflect.ValueOf(actual)

if e.Kind() != a.Kind() {
FailNow(t, "type mismatch: expected %T, got %T", expected, actual)
}

if !e.Comparable() {
FailNow(t, "expected value is not comparable")
}

if !a.Comparable() {
FailNow(t, "actual value is not comparable")
}

if e.Kind() != a.Kind() {
FailNow(t, "type mismatch: expected %T, got %T", expected, actual)
}

switch e.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
{
ev := e.Convert(reflect.TypeOf(int64(0))).Interface().(int64)
av := a.Convert(reflect.TypeOf(int64(0))).Interface().(int64)
return cmp.Compare(ev, av)
}

case reflect.Uint8, reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
{
ev := e.Convert(reflect.TypeOf(uint64(0))).Interface().(uint64)
av := a.Convert(reflect.TypeOf(uint64(0))).Interface().(uint64)
return cmp.Compare(ev, av)
}

case reflect.Float32, reflect.Float64:
{
ev := e.Convert(reflect.TypeOf(float64(0))).Interface().(float64)
av := a.Convert(reflect.TypeOf(float64(0))).Interface().(float64)
return cmp.Compare(ev, av)
}
case reflect.String:
{
ev := e.Convert(reflect.TypeOf(string(""))).Interface().(string)
av := a.Convert(reflect.TypeOf(string(""))).Interface().(string)
return cmp.Compare(ev, av)
}
case reflect.Uintptr:
{
ev := e.Convert(reflect.TypeOf(uintptr(0))).Interface().(uintptr)
av := a.Convert(reflect.TypeOf(uintptr(0))).Interface().(uintptr)
return cmp.Compare(ev, av)
}
}

FailNow(t, "type not supported: %T", expected)
return 0 // should not be reached
}
20 changes: 20 additions & 0 deletions internal/testutils/require/compare_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package require

import "testing"

func TestCompare(t *testing.T) {
GreaterOrEqual(t, 1, 2)
GreaterOrEqual(t, 1, 1)
Greater(t, 1, 2)
LessOrEqual(t, 2, 1)
LessOrEqual(t, 1, 1)
Less(t, 2, 1)

require := New(t)
require.GreaterOrEqual(1, 2)
require.GreaterOrEqual(1, 1)
require.Greater(1, 2)
require.LessOrEqual(2, 1)
require.LessOrEqual(1, 1)
require.Less(2, 1)
}
31 changes: 31 additions & 0 deletions internal/testutils/require/fail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package require

import (
"strings"
"testing"
)

func FailNow(t *testing.T, errMsg string, msgAndArgs ...any) {
t.Helper()

labeledMessages := labeledMessages{
{
label: "Error Trace",
message: strings.Join(CallStack(), "\n\t\t\t"),
},
{
label: "Error",
message: errMsg,
},
}

message := msgOrFmtMsg(msgAndArgs...)
if len(message) > 0 {
labeledMessages = append(labeledMessages, labeledMessage{
label: "Message",
message: message,
})
}

t.Fatal(labeledMessages.String())
}
71 changes: 71 additions & 0 deletions internal/testutils/require/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package require

import (
"bufio"
"fmt"
"strings"
)

func msgOrFmtMsg(msgAndArgs ...any) string {
if len(msgAndArgs) == 0 || msgAndArgs == nil {
return ""
}
if len(msgAndArgs) == 1 {
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", msg)
}
if len(msgAndArgs) > 1 {
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
return ""
}

type labeledMessage struct {
label string
message string
}

type labeledMessages []labeledMessage

func (lm labeledMessages) String() string {
longestLabel := 0
numLabels := len(lm)
msgSizeTotal := 0
for _, v := range lm {
if len(v.label) > longestLabel {
longestLabel = len(v.label)
}
msgSizeTotal += len(v.message)
}

var sb strings.Builder
sb.Grow(msgSizeTotal + numLabels*(longestLabel+8))
sb.WriteString("\n")

for _, v := range lm {
sb.WriteString("\t")
sb.WriteString(v.label)
sb.WriteString(":")
sb.WriteString(strings.Repeat(" ", longestLabel-len(v.label)))
sb.WriteString("\t")

// indent lines
for i, scanner := 0, bufio.NewScanner(strings.NewReader(v.message)); scanner.Scan(); i++ {
// no need to align first line because it starts at the correct location (after the label)
if i != 0 {
// append alignLen+1 spaces to align with "{{longestLabel}}:" before adding tab
sb.WriteString("\n\t")
sb.WriteString(strings.Repeat(" ", longestLabel+1))
sb.WriteString("\t")
}
// write line
sb.WriteString(scanner.Text())
}
sb.WriteString("\n")
}

return sb.String()
}
Loading

0 comments on commit 4ef8856

Please sign in to comment.