<commit_before>// Copyright 2014 The Cayley Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package iterator

import (
	"testing"

	"github.com/google/cayley/graph"
)

var simpleStore = &store{data: []string{"0", "1", "2", "3", "4", "5"}}

func simpleFixedIterator() *Fixed {
	f := newFixed()
	for i := 0; i < 5; i++ {
		f.Add(i)
	}
	return f
}

func checkIteratorContains(ts graph.TripleStore, it graph.Iterator, expected []string, t *testing.T) {
	var actual []string
	actual = nil
	for {
		val, ok := it.Next()
		if !ok {
			break
		}
		actual = append(actual, ts.NameOf(val))
	}
	actualSet := actual[:]
	for _, a := range expected {
		found := false
		for j, b := range actualSet {
			if a == b {
				actualSet = append(actualSet[:j], actualSet[j+1:]...)
				found = true
				break
			}
		}
		if !found {
			t.Error("Couldn't find", a, "in actual output.\nActual:", actual, "\nExpected: ", expected, "\nRemainder: ", actualSet)
			return
		}
	}
	if len(actualSet) != 0 {
		t.Error("Actual output has more than expected.\nActual:", actual, "\nExpected: ", expected, "\nRemainder: ", actualSet)
	}
}

func TestWorkingIntValueComparison(t *testing.T) {
	ts := simpleStore
	fixed := simpleFixedIterator()
	vc := NewComparison(fixed, kCompareLT, int64(3), ts)
	checkIteratorContains(ts, vc, []string{"0", "1", "2"}, t)
}

func TestFailingIntValueComparison(t *testing.T) {
	ts := simpleStore
	fixed := simpleFixedIterator()
	vc := NewComparison(fixed, kCompareLT, int64(0), ts)
	checkIteratorContains(ts, vc, []string{}, t)
}

func TestWorkingGT(t *testing.T) {
	ts := simpleStore
	fixed := simpleFixedIterator()
	vc := NewComparison(fixed, kCompareGT, int64(2), ts)
	checkIteratorContains(ts, vc, []string{"3", "4"}, t)
}

func TestWorkingGTE(t *testing.T) {
	ts := simpleStore
	fixed := simpleFixedIterator()
	vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
	checkIteratorContains(ts, vc, []string{"2", "3", "4"}, t)
}

func TestVCICheck(t *testing.T) {
	ts := simpleStore
	fixed := simpleFixedIterator()
	vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
	if vc.Check(1) {
		t.Error("1 is less than 2, should be GTE")
	}
	if !vc.Check(2) {
		t.Error("2 is GTE 2")
	}
	if !vc.Check(3) {
		t.Error("3 is GTE 2")
	}
	if vc.Check(5) {
		t.Error("5 is not in the underlying iterator")
	}
}<commit_msg>Tabulate value comparison tests<commit_after>// Copyright 2014 The Cayley Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package iterator

import (
	"reflect"
	"testing"

	"github.com/google/cayley/graph"
)

var simpleStore = &store{data: []string{"0", "1", "2", "3", "4", "5"}}

func simpleFixedIterator() *Fixed {
	f := newFixed()
	for i := 0; i < 5; i++ {
		f.Add(i)
	}
	return f
}

var comparisonTests = []struct {
	message  string
	operand  graph.Value
	operator Operator
	expect   []string
}{
	{
		message:  "successful int64 less than comparison",
		operand:  int64(3),
		operator: kCompareLT,
		expect:   []string{"0", "1", "2"},
	},
	{
		message:  "empty int64 less than comparison",
		operand:  int64(0),
		operator: kCompareLT,
		expect:   nil,
	},
	{
		message:  "successful int64 greater than comparison",
		operand:  int64(2),
		operator: kCompareGT,
		expect:   []string{"3", "4"},
	},
	{
		message:  "successful int64 greater than or equal comparison",
		operand:  int64(2),
		operator: kCompareGTE,
		expect:   []string{"2", "3", "4"},
	},
}

func TestValueComparison(t *testing.T) {
	for _, test := range comparisonTests {
		ts := simpleStore
		vc := NewComparison(simpleFixedIterator(), test.operator, test.operand, ts)

		var got []string
		for {
			val, ok := vc.Next()
			if !ok {
				break
			}
			got = append(got, ts.NameOf(val))
		}
		if !reflect.DeepEqual(got, test.expect) {
			t.Errorf("Failed to show %s, got:%q expect:%q", test.message, got, test.expect)
		}
	}
}

var vciCheckTests = []struct {
	message  string
	operator Operator
	check    graph.Value
	expect   bool
}{
	{
		message:  "1 is less than 2",
		operator: kCompareGTE,
		check:    1,
		expect:   false,
	},
	{
		message:  "2 is greater than or equal to 2",
		operator: kCompareGTE,
		check:    2,
		expect:   true,
	},
	{
		message:  "3 is greater than or equal to 2",
		operator: kCompareGTE,
		check:    3,
		expect:   true,
	},
	{
		message:  "5 is absent from iterator",
		operator: kCompareGTE,
		check:    5,
		expect:   false,
	},
}

func TestVCICheck(t *testing.T) {
	for _, test := range vciCheckTests {
		vc := NewComparison(simpleFixedIterator(), test.operator, int64(2), simpleStore)
		if vc.Check(test.check) != test.expect {
			t.Errorf("Failed to show %s", test.message)
		}
	}
}
{'fraction_non_alphanumeric': 0.14580031695721077, 'fraction_numerical': 0.019369607325233316, 'mean_word_length': 3.6216436126932465, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4547907', 'n_tokens_mistral': 2090, 'n_tokens_neox': 1954, 'n_words': 610}