<commit_before>package main

import (
	"runtime"
	"sync"
	"testing"

	"github.com/bitmark-inc/logger"
)

const (
	defaultActiveThreadCount = 0
)

var (
	stopChannel = make(chan struct{}, runtime.NumCPU())
)

func setupProofer(t *testing.T) *ProoferData {
	setupLogger(t)
	return &ProoferData{
		proofIDs:    make([]bool, runtime.NumCPU()),
		stopChannel: stopChannel,
		log:         logger.New("test"),
	}
}

func teardownProofer(p *ProoferData, threadCount uint32) {
	for i := uint32(0); i < threadCount; i++ {
		p.activeThreadDecrement(i)
	}
	logger.Finalise()
	removeTestFiles()
}

func TestActiveThreadCount(t *testing.T) {
	setupLogger(t)
	p := setupProofer(t)
	defer teardownProofer(p, defaultActiveThreadCount+1)

	threadCount := p.activeThread()
	if defaultActiveThreadCount != threadCount {
		t.Errorf("initial active thread count %d differs from %d",
			threadCount, defaultActiveThreadCount)
	}
	p.activeThreadIncrement(0)
	defer p.activeThreadDecrement(0)

	threadCount = p.activeThread()
	expected := uint32(defaultActiveThreadCount + 1)
	if expected != threadCount {
		t.Errorf("after increasing active thread, count %d different thatn expected %d",
			threadCount, expected)
	}
}

func TestNextProoferID(t *testing.T) {
	p := setupProofer(t)
	defer teardownProofer(p, defaultActiveThreadCount+1)

	totalCPU := runtime.NumCPU()

	expected := []struct {
		threadIncrementCount int
		expected             int
	}{
		{0, 0},                     // none created
		{1, 1},                     // additional 1 created, next ID will be 1
		{1, 2},                     // additional 1 created, next ID will be 2
		{totalCPU, errorProoferID}, // additional total cpu count created, next ID will be -1
	}

	for _, s := range expected {
		p.createProofer(uint32(s.threadIncrementCount))
		nextID, _ := p.nextProoferID()
		if s.expected != nextID {
			t.Errorf("error getting next proofer ID, expect %d but get %d", s.expected, nextID)
		}
	}
}

func TestProoferInitProoferIDs(t *testing.T) {
	setupLogger(t)
	p := setupProofer(t)
	defer teardownProofer(p, defaultActiveThreadCount+1)

	expected := runtime.NumCPU()
	actual := len(p.proofIDs)
	if actual != expected {
		t.Errorf("proofer init error, expect proofer ID slice size %d but get %d", expected, actual)
	}
}

func TestDifferenceToTargetThreadCount(t *testing.T) {
	cpuCount := 10
	p := &ProoferData{
		cpuCount: cpuCount,<commit_msg>[recorderd] modify proofer test<commit_after>package main

import (
	"runtime"
	"sync"
	"testing"

	"github.com/bitmark-inc/logger"
)

const (
	defaultActiveThreadCount = 0
)

var (
	stopChannel = make(chan struct{}, runtime.NumCPU())
)

func setupProofer(t *testing.T) *ProoferData {
	setupLogger(t)
	return &ProoferData{
		proofIDs:    make([]bool, runtime.NumCPU()),
		stopChannel: stopChannel,
		log:         logger.New("test"),
	}
}

func teardownProofer(p *ProoferData, threadCount uint32) {
	for i := uint32(0); i < threadCount; i++ {
		p.activeThreadDecrement(i)
	}
	logger.Finalise()
	removeTestFiles()
}

func TestActiveThreadCount(t *testing.T) {
	setupLogger(t)
	p := setupProofer(t)
	defer teardownProofer(p, defaultActiveThreadCount+1)

	threadCount := p.activeThread()
	if defaultActiveThreadCount != threadCount {
		t.Errorf("initial active thread count %d differs from %d",
			threadCount, defaultActiveThreadCount)
	}
	p.activeThreadIncrement(0)
	defer p.activeThreadDecrement(0)

	threadCount = p.activeThread()
	expected := uint32(defaultActiveThreadCount + 1)
	if expected != threadCount {
		t.Errorf("after increasing active thread, count %d different thatn expected %d",
			threadCount, expected)
	}
}

func TestNextProoferID(t *testing.T) {
	p := setupProofer(t)
	defer teardownProofer(p, defaultActiveThreadCount+1)

	expected := []struct {
		ids      []bool
		expected int
	}{
		{[]bool{true, false, true, false}, 1},
		{[]bool{true, true, true, true}, errorProoferID},
		{[]bool{false, true, false, true}, 0},
	}

	for i, s := range expected {
		p.proofIDs = make([]bool, len(s.ids))
		for idx, boolean := range s.ids {
			p.proofIDs[idx] = boolean
		}

		actual, err := p.nextProoferID()
		if actual != errorProoferID && nil != err {
			t.Errorf("%dth test fail, error message: %s", i, err.Error())
		}
		if actual != s.expected {
			t.Errorf("%dth test fail, next proofer ID, expect %d but get %d",
				i,
				s.expected,
				actual,
			)
		}
	}
}

func TestProoferInitProoferIDs(t *testing.T) {
	setupLogger(t)
	p := setupProofer(t)
	defer teardownProofer(p, defaultActiveThreadCount+1)

	expected := runtime.NumCPU()
	actual := len(p.proofIDs)
	if actual != expected {
		t.Errorf("proofer init error, expect proofer ID slice size %d but get %d", expected, actual)
	}
}

func TestDifferenceToTargetThreadCount(t *testing.T) {
	cpuCount := 10
	p := &ProoferData{
		cpuCount: cpuCount,
{'fraction_non_alphanumeric': 0.13022675265238196, 'fraction_numerical': 0.00977740794674433, 'mean_word_length': 4.104033970276008, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4019644', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1656, 'n_words': 411}