<commit_before>package capn_test

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"testing"

	capn "github.com/glycerine/go-capnproto"
	cv "github.com/smartystreets/goconvey/convey"
)

var benchForever bool

func init() {
	flag.BoolVar(&benchForever, "bench.forever", false, "benchmark forever")
	flag.Parse()
}

func TestReadFromStream(t *testing.T) {
	const n = 10
	r := zdateReader(n, false)
	s, err := capn.ReadFromStream(r, nil)
	if err != nil {
		t.Fatalf("ReadFromStream: %v", err)
	}
	z := ReadRootZ(s)
	if z.Which() != Z_ZDATEVEC {
		panic("expected Z_ZDATEVEC in root Z of segment")
	}
	zdatelist := z.Zdatevec()

	for i := 0; i < n; i++ {
		zdate := zdatelist.At(i)
		js, err := zdate.MarshalJSON()
		if err != nil {
			t.Fatalf("MarshalJSON: %v", err)
		}
		t.Logf("%s", string(js))
	}
}

func TestDecompressorZdate(t *testing.T) {
	const n = 2

	r := zdateReader(n, false)
	expected, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("ReadAll: %v", err)
	}

	r = zdateReader(n, true)
	actual, err := ioutil.ReadAll(capn.NewDecompressor(r))
	if err != nil {
		t.Fatalf("ReadAll: %v", err)
	}

	if !bytes.Equal(expected, actual) {
		fmt.Printf("expected to get: '%s'\n actually observed instead: '%s'\n", expected, actual)
		t.Fatal("decompressor failed")
	}
}

/*
func TestDecompressorZdate2(t *testing.T) {
	const n = 1

	r := zdateReader(n, false)
	expected, err := ioutil.ReadAll(r)<commit_msg>add test TestDecompressorZdate2() for n=2, the smallest n where unpacking is problematic<commit_after>package capn_test

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"testing"

	capn "github.com/glycerine/go-capnproto"
	cv "github.com/smartystreets/goconvey/convey"
)

var benchForever bool

func init() {
	flag.BoolVar(&benchForever, "bench.forever", false, "benchmark forever")
	flag.Parse()
}

func TestReadFromStream(t *testing.T) {
	const n = 10
	r := zdateReader(n, false)
	s, err := capn.ReadFromStream(r, nil)
	if err != nil {
		t.Fatalf("ReadFromStream: %v", err)
	}
	z := ReadRootZ(s)
	if z.Which() != Z_ZDATEVEC {
		panic("expected Z_ZDATEVEC in root Z of segment")
	}
	zdatelist := z.Zdatevec()

	for i := 0; i < n; i++ {
		zdate := zdatelist.At(i)
		js, err := zdate.MarshalJSON()
		if err != nil {
			t.Fatalf("MarshalJSON: %v", err)
		}
		t.Logf("%s", string(js))
	}
}

func TestDecompressorZdate1(t *testing.T) {
	const n = 1

	r := zdateReader(n, false)
	expected, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("ReadAll: %v", err)
	}

	r = zdateReader(n, true)
	actual, err := ioutil.ReadAll(capn.NewDecompressor(r))
	if err != nil {
		t.Fatalf("ReadAll: %v", err)
	}

	if !bytes.Equal(expected, actual) {
		fmt.Printf("expected to get: '%s'\n actually observed instead: '%s'\n", expected, actual)
		t.Fatal("decompressor failed")
	}
}

func TestDecompressorZdate2(t *testing.T) {
	const n = 2

	r := zdateReader(n, false)
	expected, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("ReadAll: %v", err)
	}

	//r = zdateReader(n, true)
	_, slice := zdateFilledSegment(n, true)
	r = bytes.NewReader(slice)

	fnout := "zdate2.packed.dat"
	f, err := os.Create(fnout)
	if err != nil {
		panic(err)
	}
	nbytes, err := io.Copy(f, bytes.NewReader(slice))
	if err != nil {
		panic(err)
	}
	if nbytes <= 0 {
		panic(fmt.Sprintf(fmt.Sprintf("no bytes written to '%s'", fnout)))
	}
	f.Close()
	fmt.Printf("TestDecompressorZdate2(): wroted packed bytes out to file '%s'\n", fnout)

	actual, err := ioutil.ReadAll(capn.NewDecompressor(r))
	if err != nil {
		t.Fatalf("ReadAll: %v", err)
	}

	if !bytes.Equal(expected, actual) {
		fmt.Printf("expected to get: '%s'\n actually observed instead: '%s'\n", expected, actual)
		t.Fatal("decompressor failed")
	}
}

/*
func TestDecompressorZdate2(t *testing.T) {
	const n = 1

	r := zdateReader(n, false)
	expected, err := ioutil.ReadAll(r)
{'fraction_non_alphanumeric': 0.17189132706374086, 'fraction_numerical': 0.00522466039707419, 'mean_word_length': 3.9856770833333335, 'pattern_counts': {'":': 0, '<': 6, '<?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': '1147809', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1589, 'n_words': 363}