<commit_before>package farm

import (
	"reflect"
	"strings"
	"testing"

	"github.com/soundcloud/roshi/common"
)

func TestUnionDifferenceOfOne(t *testing.T) {
	inputSet := tupleSet{
		common.KeyScoreMember{Key: "a", Score: 5, Member: "a"}: struct{}{},
		common.KeyScoreMember{Key: "b", Score: 3, Member: "b"}: struct{}{},
		common.KeyScoreMember{Key: "c", Score: 1, Member: "c"}: struct{}{},
	}
	union, difference := unionDifference([]tupleSet{inputSet})
	if expected, got := inputSet, union; !reflect.DeepEqual(expected, got) {
		t.Errorf("union: expected %v, got %v", expected, got)
	}
	if len(difference) != 0 {
		t.Errorf("difference: expected empty set, got %v", difference)
	}
}

func TestUnionDifferenceFullResponse(t *testing.T) {
	for input, expected := range map[string]string{
		//
		//   cluster 1  A B C  A B C  A B C  A B -  A - -  A B -  A B -  A B -
		//   cluster 2  A B C  A B C  A B -  A - -  - - -  A - -  A - C  A - C
		//   cluster 3  A B C  A B    A - -  - - -  - - -  - - C  - B -  - B C
		//  ==========  =====  =====  =====  =====  =====  =====  =====  =====
		//       union  A B C  A B C  A B C  A B -  A - -  A B C  A B C  A B C
		//  difference  - - -  - - C  - B C  A B -  A - -  A B C  A B C  A B C
		//
		"A5B3C1 A5B3C1 A5B3C1": "A5B3C1 / ---",
		"A5B3C1 A5B3C1 A5B3--": "A5B3C1 / --C",
		"A5B3C1 A5B3-- A5----": "A5B3C1 / -BC",
		"A5B3-- A5---- ------": "A5B3-- / AB-",
		"A5---- ------ ------": "A5---- / A--",<commit_msg>TestWriteOnly<commit_after>package farm

import (
	"reflect"
	"strings"
	"sync/atomic"
	"testing"

	"github.com/soundcloud/roshi/common"
	"github.com/soundcloud/roshi/instrumentation"
)

func TestWriteOnly(t *testing.T) {
	var (
		nClusters   = 5
		allClusters = newMockClusters(nClusters)
		farm        = New(allClusters, allClusters[1:], nClusters, SendAllReadAll, NoRepairs, instrumentation.NopInstrumentation{})
	)

	// Write.
	if err := farm.Insert([]common.KeyScoreMember{
		{Key: "a", Score: 1, Member: "a"},
	}); err != nil {
		t.Fatal(err)
	}

	// Write should go to all clusters.
	for i, cluster := range allClusters {
		if expected, got := int32(1), atomic.LoadInt32(&(cluster.(*mockCluster).countInsert)); expected != got {
			t.Errorf("cluster %d countInsert: expected %d, got %d", i+1, expected, got)
		}
	}

	// Read.
	if _, err := farm.Select([]string{"a"}, 0, 10); err != nil {
		t.Fatal(err)
	}

	// Read should go only to the read clusters.
	if expected, got := int32(0), atomic.LoadInt32(&(allClusters[0].(*mockCluster).countSelect)); expected != got {
		t.Errorf("write cluster countSelect: expected %d, got %d", expected, got)
	}
	for i, cluster := range allClusters[1:] {
		if expected, got := int32(1), atomic.LoadInt32(&(cluster.(*mockCluster).countSelect)); expected != got {
			t.Errorf("read cluster %d countSelect: expected %d, got %d", i+1, expected, got)
		}
	}
}

func TestUnionDifferenceOfOne(t *testing.T) {
	inputSet := tupleSet{
		common.KeyScoreMember{Key: "a", Score: 5, Member: "a"}: struct{}{},
		common.KeyScoreMember{Key: "b", Score: 3, Member: "b"}: struct{}{},
		common.KeyScoreMember{Key: "c", Score: 1, Member: "c"}: struct{}{},
	}
	union, difference := unionDifference([]tupleSet{inputSet})
	if expected, got := inputSet, union; !reflect.DeepEqual(expected, got) {
		t.Errorf("union: expected %v, got %v", expected, got)
	}
	if len(difference) != 0 {
		t.Errorf("difference: expected empty set, got %v", difference)
	}
}

func TestUnionDifferenceFullResponse(t *testing.T) {
	for input, expected := range map[string]string{
		//
		//   cluster 1  A B C  A B C  A B C  A B -  A - -  A B -  A B -  A B -
		//   cluster 2  A B C  A B C  A B -  A - -  - - -  A - -  A - C  A - C
		//   cluster 3  A B C  A B    A - -  - - -  - - -  - - C  - B -  - B C
		//  ==========  =====  =====  =====  =====  =====  =====  =====  =====
		//       union  A B C  A B C  A B C  A B -  A - -  A B C  A B C  A B C
		//  difference  - - -  - - C  - B C  A B -  A - -  A B C  A B C  A B C
		//
		"A5B3C1 A5B3C1 A5B3C1": "A5B3C1 / ---",
		"A5B3C1 A5B3C1 A5B3--": "A5B3C1 / --C",
		"A5B3C1 A5B3-- A5----": "A5B3C1 / -BC",
		"A5B3-- A5---- ------": "A5B3-- / AB-",
		"A5---- ------ ------": "A5---- / A--",
{'fraction_non_alphanumeric': 0.2109916367980884, 'fraction_numerical': 0.02795698924731183, 'mean_word_length': 2.362248995983936, 'pattern_counts': {'":': 10, '<': 3, '<?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': '1729127', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1693, 'n_words': 463}