Joyit 1 mēnesi atpakaļ
vecāks
revīzija
f64ca6d7e1
2 mainītis faili ar 137 papildinājumiem un 17 dzēšanām
  1. 11 17
      util/encrypt_test.go
  2. 126 0
      util/pool/pool_test.go

+ 11 - 17
util/encrypt_test.go

@@ -1,29 +1,23 @@
 package util
 
 import (
-	"log"
+	"slices"
 	"testing"
 )
 
-/*
-func TestDecodeBasic(t *testing.T) {
-	tests := []struct {
-		name        string
-		header      http.Header
-		expUsername string
-		expPassword string
-	}{
-*/
-
 func TestMd5Sum(t *testing.T) {
 	test := []byte("hello")
-	m5 := Md5Sum(test)
-	// [93, 65, 64, 42, 188, 75, 42, 118, 185, 113, 157, 145, 16, 23, 197, 146]
-	log.Println(m5)
+	get := Md5Sum(test)
+	want := []byte{93, 65, 64, 42, 188, 75, 42, 118, 185, 113, 157, 145, 16, 23, 197, 146}
+	if !slices.Equal(get, want) {
+		t.Fatal("failed")
+	}
 }
 
 func TestEvpBytesToKey(t *testing.T) {
-	key := EvpBytesToKey("hello", 32)
-	// [93, 65, 64, 42, 188, 75, 42, 118, 185, 113, 157, 145, 16, 23, 197, 146, 40, 180, 110, 211, 193, 17, 232, 81, 2, 144, 155, 28, 251, 80, 234, 15]
-	log.Println(key)
+	get := EvpBytesToKey("hello", 32)
+	want := []byte{93, 65, 64, 42, 188, 75, 42, 118, 185, 113, 157, 145, 16, 23, 197, 146, 40, 180, 110, 211, 193, 17, 232, 81, 2, 144, 155, 28, 251, 80, 234, 15}
+	if !slices.Equal(get, want) {
+		t.Fatal("failed")
+	}
 }

+ 126 - 0
util/pool/pool_test.go

@@ -0,0 +1,126 @@
+package pool
+
+import (
+	"math/bits"
+	"math/rand"
+	"testing"
+)
+
+func TestGet(t *testing.T) {
+	if Get(0) != nil {
+		t.Fatal(0)
+		return
+	}
+	if len(Get(1)) != 1 {
+		t.Fatal(1)
+		return
+	}
+	if len(Get(2)) != 2 {
+		t.Fatal(2)
+		return
+	}
+	if len(Get(3)) != 3 || cap(Get(3)) != 64 {
+		t.Fatal(3)
+		return
+	}
+	if len(Get(4)) != 4 {
+		t.Fatal(4)
+		return
+	}
+	if len(Get(1023)) != 1023 || cap(Get(1023)) != 1024 {
+		t.Fatal(1023)
+		return
+	}
+	if len(Get(1024)) != 1024 {
+		t.Fatal(1024)
+		return
+	}
+	if len(Get(65536)) != 65536 {
+		t.Fatal(65536)
+		return
+	}
+}
+
+func TestPut(t *testing.T) {
+	if err := Put(nil); err == nil {
+		t.Fatal("put nil misbehavior")
+		return
+	}
+	b := make([]byte, 3)
+	if err := Put(b); err == nil {
+		t.Fatal("put elem:3 []bytes misbehavior")
+		return
+	}
+	b = make([]byte, 64)
+	if err := Put(b); err != nil {
+		t.Fatal("put elem:4 []bytes misbehavior")
+		return
+	}
+	b = make([]byte, 1023, 1024)
+	if err := Put(b); err != nil {
+		t.Fatal("put elem:1024 []bytes misbehavior")
+		return
+	}
+	b = make([]byte, 65536)
+	if err := Put(b); err != nil {
+		t.Fatal("put elem:65536 []bytes misbehavior")
+		return
+	}
+}
+
+func TestPutThenGet(t *testing.T) {
+	data := Get(4)
+	Put(data)
+	newData := Get(4)
+	if cap(data) != cap(newData) {
+		t.Fatal("different cap while Get()")
+		return
+	}
+}
+
+func BenchmarkMSB(b *testing.B) {
+	for b.Loop() {
+		msb(rand.Int())
+	}
+}
+
+func BenchmarkAlloc(b *testing.B) {
+	for i := 0; b.Loop(); i++ {
+		pbuf := Get(i % 65536)
+		Put(pbuf)
+	}
+}
+
+func TestDebrujin(t *testing.T) {
+	for i := 1; i <= 65536; i++ {
+		a := int(msb(i))
+		b := bits.Len(uint(i))
+		if a+1 != b {
+			t.Fatal("debrujin")
+			return
+		}
+	}
+}
+
+func TestPutSizeMismatch(t *testing.T) {
+	data := Get(1024)
+	if data == nil {
+		t.Fatal("Get(1024) failed")
+	}
+
+	// simulate a slice operation that reduces capacity
+	// e.g. data[1:]
+	// cap becomes 1023
+	sliced := (data)[1:]
+	if err := Put(sliced); err == nil {
+		t.Fatal("Put() should fail with mismatched capacity")
+	}
+
+	// simulate a slice operation that keeps capacity
+	// e.g. data[:512]
+	// cap remains 1024
+	sliced = (data)[:512]
+	if err := Put(sliced); err != nil {
+		t.Fatal("Put() should succeed with matched capacity")
+	}
+}