<commit_before>package connectivity

import (
	"sort"
	"testing"
)

// test set is a struct of set/bag of items, plus
// the test subject -> 2 items
// and expected output -> true or false
type TestSet struct {
	Exp      bool
	SubjectA int
	SubjectB int
	Set      Component
}

// prepare test sets
func getTestSets() *[]TestSet {
	return &[]TestSet{
		{
			Exp:      true,
			SubjectA: 2,
			SubjectB: 3,
			Set:      []int{2, 3, 6, 7},
		},
		{
			Exp:      true,
			SubjectA: 1,
			SubjectB: 4,
			Set:      []int{1, 4, 5},
		},
		{
			Exp:      false,
			SubjectA: 2,
			SubjectB: 9,<commit_msg>added test data from algs4.cs.princeton.edu test revealed bugs in original implementation<commit_after>package connectivity

import (
	"bufio"
	"log"
	"net/http"
	"sort"
	"strconv"
	"testing"
)

// download test data from internet
const (
	tiny   string = "http://algs4.cs.princeton.edu/15uf/tinyUF.txt"
	medium string = "http://algs4.cs.princeton.edu/15uf/mediumUF.txt"
	// huge   string = "http://algs4.cs.princeton.edu/15uf/largeUF.txt"
)

func loadTestDataFromWeb() []TestData {
	cl := &http.Client{}
	tr, err := cl.Get(tiny)
	if err != nil {
		log.Fatalln(err)
	}
	mr, err := cl.Get(medium)
	if err != nil {
		log.Fatalln(err)
	}
	// hr, err := cl.Get(huge)
	// if err != nil {
	// 	log.Fatalln(err)
	// }

	c := make(chan TestData)
	// go func() { c <- parse(hr) }()
	go func() { c <- parse(mr) }()
	go func() { c <- parse(tr) }()

	var results []TestData
	for i := 0; i < 2; /*3*/ i++ {
		result := <-c
		results = append(results, result)
	}
	return results
}

func parse(r *http.Response) (result TestData) {
	defer r.Body.Close()

	s := bufio.NewScanner(r.Body)
	s.Split(bufio.ScanWords)
	var pairs []Pair
	s.Scan()
	g, err := strconv.Atoi(s.Text())
	if err != nil {
		log.Panicf("first generator parse error %v", g, err)
	}
	for s.Scan() {
		var pair Pair
		pair.Left, err = strconv.Atoi(s.Text())
		if err != nil {
			log.Fatalf("parse left %v failed", pair.Left, err)
		}
		s.Scan()
		pair.Right, err = strconv.Atoi(s.Text())
		if err != nil {
			log.Fatalf("parse right %v failed", pair.Right, err)
		}
		pairs = append(pairs, pair)
	}

	result.Generator = g
	result.Pairs = pairs
	return result
}

type Pair struct {
	Left  int
	Right int
}

type TestData struct {
	Generator int
	Pairs     []Pair
}

func TestJustTest(t *testing.T) {
	n := loadTestDataFromWeb()
	for _, content := range n {
		log.Printf("Generator %v has %v pairs", content.Generator, len(content.Pairs))
		test := NewConnectivity(content.Generator)
		for idx, p := range content.Pairs {
			// log.Printf("%vth pair: %v and %v", idx, p.Left, p.Right)
			test.Union(p.Left, p.Right)
			if !test.FindQuery(p.Left, p.Right) {
				t.Errorf("%v and %v are expected to be in the same component, this is the %vth union operation", p.Left, p.Right, idx)
			}
		}
	}
}

// test set is a struct of set/bag of items, plus
// the test subject -> 2 items
// and expected output -> true or false
type TestSet struct {
	Exp      bool
	SubjectA int
	SubjectB int
	Set      Component
}

// prepare test sets
func getTestSets() *[]TestSet {
	return &[]TestSet{
		{
			Exp:      true,
			SubjectA: 2,
			SubjectB: 3,
			Set:      []int{2, 3, 6, 7},
		},
		{
			Exp:      true,
			SubjectA: 1,
			SubjectB: 4,
			Set:      []int{1, 4, 5},
		},
		{
			Exp:      false,
			SubjectA: 2,
			SubjectB: 9,
{'fraction_non_alphanumeric': 0.15599880916939565, 'fraction_numerical': 0.01220601369455195, 'mean_word_length': 2.7291897891231964, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '936838', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1302, 'n_words': 366}