Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added unit test for prof package #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions lib/others/prof/profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package prof

import (
"reflect"
"testing"
"time"
)

func TestSortif(t *testing.T) {
data := []oneval{
{name: "A", tim: 10},
{name: "B", tim: 5},
{name: "C", tim: 15},
}

s := sortif{val: data}

expectedLen := len(data)
if gotLen := s.Len(); gotLen != expectedLen {
t.Errorf("Length mismatch, expected: %d, got: %d", expectedLen, gotLen)
}

if !s.Less(0, 1) {
t.Error("Expected first element to be less than the second one")
}
if s.Less(1, 0) {
t.Error("Expected first element to be greater than the second one")
}

expectedData := []oneval{
{name: "B", tim: 5},
{name: "A", tim: 10},
{name: "C", tim: 15},
}
s.Swap(0, 1)
if !reflect.DeepEqual(s.val, expectedData) {
t.Errorf("Swap result mismatch, expected: %+v, got: %+v", expectedData, s.val)
}
}

func TestStaSto(t *testing.T) {
Start()

// Test Sta and Sto functions
name := "testCheckpoint"
Sta(name)
time.Sleep(100 * time.Millisecond)
Sto(name)
// If Sta and Sto work correctly, no panic should occur
}

func TestStop(t *testing.T) {
Start()

// Test Stop function
Stop()
// If Stop works correctly, no panic should occur
}

func TestStart(t *testing.T) {
// Test Start function
Start()
// If Start works correctly, no panic should occur
}

func TestProfilerDisabled(t *testing.T) {
// Test ProfilerDisabled flag
if ProfilerDisabled {
t.Error("Profiler should not be disabled initially")
}

ProfilerDisabled = true
// If ProfilerDisabled is set to true, it should not panic
}