1- // Unit tests for the Go implementation of Huffman Coding
21package huffman
32
43import (
54 "bufio"
6- "fmt "
5+ "errors "
76 "io"
87 "os"
98 "path/filepath"
109 "testing"
10+
11+ "github.com/stretchr/testify/require"
1112)
1213
1314const (
@@ -16,12 +17,9 @@ const (
1617 testUncompressedFilename = "plrabn12_compressed_uncompressed.txt"
1718)
1819
19- // Ensures that the original file matches the uncompressed file
20- func TestCompareFiles (t * testing.T ) {
20+ func TestHuffmanCoding (t * testing.T ) {
2121 cwd , err := os .Getwd ()
22- if err != nil {
23- t .Errorf ("Unable to determine the current working directory. err: %v" , err )
24- }
22+ require .NoError (t , err , "Failed to get the working directory" )
2523 testDir := filepath .Join (cwd , ".." , "test" )
2624
2725 testFilepath := filepath .Join (testDir , testFilename )
@@ -30,43 +28,40 @@ func TestCompareFiles(t *testing.T) {
3028 defer os .Remove (testCompressedFilepath )
3129 defer os .Remove (testUncompressedFilepath )
3230
33- // Compress and uncompress the files first
34- Compress (testFilepath )
35- Uncompress (testCompressedFilepath )
31+ t .Run ("Compress" , func (t * testing.T ) {
32+ err = Compress (testFilepath )
33+ require .NoError (t , err , "Failed to compress the test file" )
34+ })
35+
36+ t .Run ("Uncompress" , func (t * testing.T ) {
37+ err = Uncompress (testCompressedFilepath )
38+ require .NoError (t , err , "Failed to uncompress the compressed file" )
39+ })
3640
37- // Open both files
3841 originalFile , err := os .Open (testFilepath )
39- if err != nil {
40- fmt .Println (err )
41- t .Error ("There was an error opening the original file!" )
42- return
43- }
42+ require .NoError (t , err , "Failed to open the test file" )
4443 defer originalFile .Close ()
4544
4645 uncompressedFile , err := os .Open (testUncompressedFilepath )
47- if err != nil {
48- fmt .Println (err )
49- t .Error ("There was an error opening the uncompressed file!" )
50- }
46+ require .NoError (t , err , "Failed to open the uncompressed file" )
5147 defer uncompressedFile .Close ()
5248
5349 originalReader := bufio .NewReader (originalFile )
5450 uncompressedReader := bufio .NewReader (uncompressedFile )
5551
56- for {
57- originalByte , err1 := originalReader .ReadByte ()
58- uncompressedByte , err2 := uncompressedReader .ReadByte ()
59- if err1 == io .EOF && err2 == io .EOF {
60- break
61- } else if err1 == io .EOF && err2 != io .EOF {
62- t .Error ("The two files are not the same length" )
63- } else if err1 != io .EOF && err2 == io .EOF {
64- t .Error ("The two files are not the same length" )
65- }
52+ t .Run ("CompareFiles" , func (t * testing.T ) {
53+ for {
54+ originalByte , err1 := originalReader .ReadByte ()
55+ uncompressedByte , err2 := uncompressedReader .ReadByte ()
56+ if errors .Is (err1 , io .EOF ) && errors .Is (err2 , io .EOF ) {
57+ break
58+ }
6659
67- if originalByte != uncompressedByte {
68- t .Error ("The two files are not equivalent" )
69- return
60+ require .NotErrorIs (t , err1 , io .EOF , "Test file is shorter than the uncompressed file" )
61+ require .NotErrorIs (t , err2 , io .EOF , "Uncompressed file is shorter than the test file" )
62+ require .NoError (t , err1 , "Failed to read byte from the test file" )
63+ require .NoError (t , err2 , "Failed to read byte from the uncompressed file" )
64+ require .Equal (t , originalByte , uncompressedByte , "Unequal character between the two files" )
7065 }
71- }
66+ })
7267}
0 commit comments