You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.3 KiB

7 years ago
  1. package words
  2. import (
  3. "testing"
  4. asrt "github.com/stretchr/testify/assert"
  5. cmn "github.com/tendermint/tmlibs/common"
  6. )
  7. var codecs = []ECC{
  8. NewIBMCRC16(),
  9. NewSCSICRC16(),
  10. NewCCITTCRC16(),
  11. NewIEEECRC32(),
  12. NewCastagnoliCRC32(),
  13. NewKoopmanCRC32(),
  14. NewISOCRC64(),
  15. NewECMACRC64(),
  16. }
  17. // TestECCPasses makes sure that the AddECC/CheckECC methods are symetric
  18. func TestECCPasses(t *testing.T) {
  19. assert := asrt.New(t)
  20. checks := append(codecs, NoECC{})
  21. for _, check := range checks {
  22. for i := 0; i < 2000; i++ {
  23. numBytes := cmn.RandInt()%60 + 1
  24. data := cmn.RandBytes(numBytes)
  25. checked := check.AddECC(data)
  26. res, err := check.CheckECC(checked)
  27. if assert.Nil(err, "%#v: %+v", check, err) {
  28. assert.Equal(data, res, "%v", check)
  29. }
  30. }
  31. }
  32. }
  33. // TestECCFails makes sure random data will (usually) fail the checksum
  34. func TestECCFails(t *testing.T) {
  35. assert := asrt.New(t)
  36. checks := codecs
  37. attempts := 2000
  38. for _, check := range checks {
  39. failed := 0
  40. for i := 0; i < attempts; i++ {
  41. numBytes := cmn.RandInt()%60 + 1
  42. data := cmn.RandBytes(numBytes)
  43. _, err := check.CheckECC(data)
  44. if err != nil {
  45. failed += 1
  46. }
  47. }
  48. // we allow up to 1 falsely accepted checksums, as there are random matches
  49. assert.InDelta(attempts, failed, 1, "%v", check)
  50. }
  51. }