github.com/ipld/go-ipld-prime@v0.21.0/node/tests/corpus/corpus.go (about)

     1  /*
     2  The corpus package exports some values useful for building tests and benchmarks.
     3  
     4  Values come as JSON strings.  It is assumed you can unmarshal those.
     5  The reason we do this is so that this corpus package doesn't import
     6  any particular concrete implementation of ipld.Node... since that would
     7  make it ironically incapable of being used for that Node's tests.
     8  
     9  The naming convention is roughly as follows:
    10  
    11    - {Kind}{{Count}|N}{KeyKind}{ValueKind}
    12    - 'Kind' is usually 'Map' or 'List'.
    13      It can also be a scalar like 'Int', in which case that's it.
    14    - If a specific int is given for 'Count', that's the size of the thing;
    15    - if 'N' is present, it's a scalable corpus and you can decide the size.
    16    - 'KeyKind' is present for maps (it will be string...).
    17    - 'ValueKind' is present for maps and lists.  It can recurse.
    18  
    19  Of course, this naming convention is not perfectly specific,
    20  but it's usually enough for our needs, or at least enough to get started.
    21  Some corpuses designed for probing (for example) tuple-represented structs
    22  will end up with interesting designations for various reasons:
    23  
    24    - some corpuses are meant to test struct semantics.
    25      This is usually what it means when you see fixed size maps.
    26      "List5Various" can also be this reason (it's for tuple-represented structs).
    27    - some corpuses are meant to test nullable or optional semantics.
    28      These might have name suffixes like "WithNull" to indicate this.
    29  
    30  Everything is exported as a function, for consistency.
    31  Many functions need no args.
    32  Some functions need an argument for "N".
    33  
    34  If you're using these corpuses in a benchmark, don't forget to call
    35  `b.ResetTimer()` after getting the corpus.
    36  */
    37  package corpus
    38  
    39  import (
    40  	"fmt"
    41  )
    42  
    43  func Map3StrInt() string {
    44  	return `{"whee":1,"woot":2,"waga":3}`
    45  }
    46  
    47  func MapNStrInt(n int) string {
    48  	return `{` + ents(n, func(i int) string {
    49  		return fmt.Sprintf(`"k%d":%d`, i, i)
    50  	}) + `}`
    51  }
    52  
    53  func MapNStrMap3StrInt(n int) string {
    54  	return `{` + ents(n, func(i int) string {
    55  		return fmt.Sprintf(`"k%d":`, i) +
    56  			fmt.Sprintf(`{"whee":%d,"woot":%d,"waga":%d}`, i*3+1, i*3+2, i*3+3)
    57  	}) + `}`
    58  }