~rvb/gwacl/image-fix

« back to all changes in this revision

Viewing changes to testhelpers_factory.go

  • Committer: Tarmac
  • Author(s): Jeroen Vermeulen
  • Date: 2013-04-25 02:31:28 UTC
  • mfrom: (93.2.1 extract-roundtrippers)
  • Revision ID: tarmac-20130425023128-n8nag1qdnmfghlz9
[r=rvb][bug=][author=jtv] Extract a bunch of our test helpers into separate files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.  This software is licensed under the
 
2
// GNU Lesser General Public License version 3 (see the file COPYING).
 
3
//
 
4
// Factories for various types of objects that tests need to create.
 
5
 
 
6
package gwacl
 
7
 
 
8
import (
 
9
    "math/rand"
 
10
    "time"
 
11
)
 
12
 
 
13
// This should be refactored at some point, it does not belong in here.
 
14
// Perhaps we can add it to gocheck, or start a testtools-like project.
 
15
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"
 
16
 
 
17
// MakeRandomString returns an arbitrary string of alphanumerical characters.
 
18
// TODO: This isn't really a random string, more of a random identifier.
 
19
func MakeRandomString(length int) string {
 
20
    return string(MakeRandomByteSlice(length))
 
21
}
 
22
 
 
23
// MakeRandomString returns a slice of arbitrary bytes, all corresponding to
 
24
// alphanumerical characters in ASCII.
 
25
// TODO: This isn't really very random.  Good tests need zero and "high" values.
 
26
func MakeRandomByteSlice(length int) []byte {
 
27
    dest := make([]byte, length)
 
28
    for i := range dest {
 
29
        num := rand.Intn(len(chars))
 
30
        rand_char := chars[num]
 
31
        dest[i] = rand_char
 
32
    }
 
33
    return dest
 
34
}
 
35
 
 
36
// MakeRandomBool returns an arbitrary bool value (true or false).
 
37
func MakeRandomBool() bool {
 
38
    v := rand.Intn(2)
 
39
    if v == 0 {
 
40
        return false
 
41
    }
 
42
    return true
 
43
}
 
44
 
 
45
func init() {
 
46
    // Seed the pseudo-random number generator.  Without this, each test run
 
47
    // will get the same sequence of results from the math/rand package.
 
48
    rand.Seed(int64(time.Now().Nanosecond()))
 
49
}