<commit_before>// Copyright © 2015 <NAME> <<EMAIL>>
//
// License for use of this code is detailed in the LICENSE file

package opus

import (
	"bytes"
	"io"
	"os"
	"strings"
	"testing"
)

func TestStreamIllegal(t *testing.T) {
	// Simple testing, no actual decoding
	reader := strings.NewReader("hello test test this is not a legal Opus stream")
	_, err := NewStream(reader)
	if err == nil {
		t.Fatalf("Expected error while initializing illegal opus stream")
	}
}

func readStreamWav(t *testing.T, stream *Stream, buffersize int) []byte {
	var buf bytes.Buffer
	pcm := make([]int16, buffersize)
	for {
		n, err := stream.Read(pcm)
		switch err {
		case io.EOF:
			return buf.Bytes()
		case nil:
			break
		default:
			t.Fatalf("Error while decoding opus file: %v", err)
		}
		if n == 0 {
			t.Fatal("Nil-error Read() must not return 0")
		}
		for i := 0; i < n; i++ {
			buf.WriteByte(byte(pcm[i] & 0xff))
			buf.WriteByte(byte(pcm[i] >> 8))
		}
	}
}

func readFileWav(t *testing.T, fname string, buffersize int) []byte {
	reader, err := os.Open(fname)
	if err != nil {
		t.Fatalf("Error while opening file: %v", err)
	}
	stream, err := NewStream(reader)
	if err != nil {
		t.Fatalf("Error while creating opus stream: %v", err)
	}
	return readStreamWav(t, stream, buffersize)
}

func TestStream(t *testing.T) {
	wav := readFileWav(t, "testdata/speech_8.opus", 10000)
	if len(wav) != 1036800 {
		t.Fatalf("Unexpected length of WAV file: %d", len(wav))
	}
}

func TestStreamSmallBuffer(t *testing.T) {
	smallbuf := readFileWav(t, "testdata/speech_8.opus", 1)
	bigbuf := readFileWav(t, "testdata/speech_8.opus", 10000)
	if !bytes.Equal(smallbuf, bigbuf) {
		t.Errorf("Reading with 1-sample buffer size yields different audio data")
	}
}

func TestCloser(t *testing.T) {
	/* TODO: test if stream.Close() also closes the underlying reader */
}<commit_msg>Test opus stream .Close method<commit_after>// Copyright © 2015 <NAME> <<EMAIL>>
//
// License for use of this code is detailed in the LICENSE file

package opus

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"strings"
	"testing"
)

func TestStreamIllegal(t *testing.T) {
	// Simple testing, no actual decoding
	reader := strings.NewReader("hello test test this is not a legal Opus stream")
	_, err := NewStream(reader)
	if err == nil {
		t.Fatalf("Expected error while initializing illegal opus stream")
	}
}

func readStreamWav(t *testing.T, stream *Stream, buffersize int) []byte {
	var buf bytes.Buffer
	pcm := make([]int16, buffersize)
	for {
		n, err := stream.Read(pcm)
		switch err {
		case io.EOF:
			return buf.Bytes()
		case nil:
			break
		default:
			t.Fatalf("Error while decoding opus file: %v", err)
		}
		if n == 0 {
			t.Fatal("Nil-error Read() must not return 0")
		}
		for i := 0; i < n; i++ {
			buf.WriteByte(byte(pcm[i] & 0xff))
			buf.WriteByte(byte(pcm[i] >> 8))
		}
	}
}

func mustOpenFile(t *testing.T, fname string) *os.File {
	f, err := os.Open(fname)
	if err != nil {
		t.Fatalf("Error while opening %s: %v", fname, err)
		return nil
	}
	return f
}

func mustOpenStream(t *testing.T, r io.Reader) *Stream {
	stream, err := NewStream(r)
	if err != nil {
		t.Fatalf("Error while creating opus stream: %v", err)
		return nil
	}
	return stream
}

func readFileWav(t *testing.T, fname string, buffersize int) []byte {
	reader := mustOpenFile(t, fname)
	stream, err := NewStream(reader)
	if err != nil {
		t.Fatalf("Error while creating opus stream: %v", err)
	}
	return readStreamWav(t, stream, buffersize)
}

func TestStream(t *testing.T) {
	wav := readFileWav(t, "testdata/speech_8.opus", 10000)
	if len(wav) != 1036800 {
		t.Fatalf("Unexpected length of WAV file: %d", len(wav))
	}
}

func TestStreamSmallBuffer(t *testing.T) {
	smallbuf := readFileWav(t, "testdata/speech_8.opus", 1)
	bigbuf := readFileWav(t, "testdata/speech_8.opus", 10000)
	if !bytes.Equal(smallbuf, bigbuf) {
		t.Errorf("Reading with 1-sample buffer size yields different audio data")
	}
}

type mockCloser struct {
	io.Reader
	closed bool
}

func (m *mockCloser) Close() error {
	if m.closed {
		return fmt.Errorf("Already closed")
	}
	m.closed = true
	return nil
}

func TestCloser(t *testing.T) {
	f := mustOpenFile(t, "testdata/speech_8.opus")
	mc := &mockCloser{Reader: f}
	stream := mustOpenStream(t, mc)
	stream.Close()
	if !mc.closed {
		t.Error("Expected opus stream to call .Close on the reader")
	}
}
{'fraction_non_alphanumeric': 0.14649827784156141, 'fraction_numerical': 0.015384615384615385, 'mean_word_length': 3.834628190899001, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '177351', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1624, 'n_words': 474}