github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/randutil/rand_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package randutil_test
    21  
    22  import (
    23  	"math/rand"
    24  	"testing"
    25  	"time"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/randutil"
    30  )
    31  
    32  func Test(t *testing.T) { TestingT(t) }
    33  
    34  type randutilSuite struct{}
    35  
    36  var _ = Suite(&randutilSuite{})
    37  
    38  func (s *randutilSuite) TestRandomString(c *C) {
    39  	// for our tests
    40  	rand.Seed(1)
    41  
    42  	s1 := randutil.RandomString(10)
    43  	c.Assert(s1, Equals, "pw7MpXh0JB")
    44  
    45  	s2 := randutil.RandomString(5)
    46  	c.Assert(s2, Equals, "4PQyl")
    47  }
    48  
    49  func (s *randutilSuite) TestRandomDuration(c *C) {
    50  	// ensure moreMixedSeed is done
    51  	d := randutil.RandomDuration(time.Hour)
    52  	c.Check(d < time.Hour, Equals, true)
    53  
    54  	// for our tests
    55  	rand.Seed(1)
    56  
    57  	d1 := randutil.RandomDuration(time.Hour)
    58  	c.Assert(d1, Equals, time.Duration(1991947779410))
    59  
    60  	d2 := randutil.RandomDuration(4 * time.Hour)
    61  	c.Assert(d2, Equals, time.Duration(4423082153551))
    62  }