pkg.re/essentialkaos/ek.v11@v12.41.0+incompatible/hash/hast_test.go (about)

     1  package hash
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"io/ioutil"
    12  	"testing"
    13  
    14  	. "pkg.re/essentialkaos/check.v1"
    15  )
    16  
    17  // ////////////////////////////////////////////////////////////////////////////////// //
    18  
    19  func Test(t *testing.T) { TestingT(t) }
    20  
    21  type HashSuite struct {
    22  	TmpDir string
    23  }
    24  
    25  // ////////////////////////////////////////////////////////////////////////////////// //
    26  
    27  var _ = Suite(&HashSuite{})
    28  
    29  // ////////////////////////////////////////////////////////////////////////////////// //
    30  
    31  func (s *HashSuite) SetUpSuite(c *C) {
    32  	s.TmpDir = c.MkDir()
    33  }
    34  
    35  func (s *HashSuite) TestJCHash(c *C) {
    36  	c.Assert(JCHash(1, 1), Equals, 0)
    37  	c.Assert(JCHash(36, 49), Equals, 8)
    38  	c.Assert(JCHash(0xDEAD10CC, 1), Equals, 0)
    39  	c.Assert(JCHash(0xDEAD10CC, 1000), Equals, 361)
    40  	c.Assert(JCHash(128, 1024), Equals, 267)
    41  }
    42  
    43  func (s *HashSuite) TestJCHashNegative(c *C) {
    44  	c.Assert(JCHash(0, -10), Equals, 0)
    45  	c.Assert(JCHash(0xDEAD10CC, -1000), Equals, 0)
    46  }
    47  
    48  func (s *HashSuite) TestFileHash(c *C) {
    49  	tempFile := s.TmpDir + "/test.log"
    50  
    51  	err := ioutil.WriteFile(tempFile, []byte("ABCDEF12345\n\n"), 0644)
    52  
    53  	hash1 := FileHash(tempFile)
    54  	hash2 := FileHash(s.TmpDir + "/not-exist.log")
    55  
    56  	c.Assert(err, IsNil)
    57  	c.Assert(hash1, Equals, "2d7ec20906125cd23fee7b628b98463d554b1105b141b2d39a19bac5f3274dec")
    58  	c.Assert(hash2, Equals, "")
    59  }