t / t0011-hashmap.shon commit userdiff: add a builtin pattern for dts files (3c81760)
   1#!/bin/sh
   2
   3test_description='test hashmap and string hash functions'
   4. ./test-lib.sh
   5
   6test_hashmap() {
   7        echo "$1" | test-tool hashmap $3 > actual &&
   8        echo "$2" > expect &&
   9        test_cmp expect actual
  10}
  11
  12test_expect_success 'put' '
  13
  14test_hashmap "put key1 value1
  15put key2 value2
  16put fooBarFrotz value3
  17put foobarfrotz value4
  18size" "NULL
  19NULL
  20NULL
  21NULL
  2264 4"
  23
  24'
  25
  26test_expect_success 'put (case insensitive)' '
  27
  28test_hashmap "put key1 value1
  29put key2 value2
  30put fooBarFrotz value3
  31size" "NULL
  32NULL
  33NULL
  3464 3" ignorecase
  35
  36'
  37
  38test_expect_success 'replace' '
  39
  40test_hashmap "put key1 value1
  41put key1 value2
  42put fooBarFrotz value3
  43put fooBarFrotz value4
  44size" "NULL
  45value1
  46NULL
  47value3
  4864 2"
  49
  50'
  51
  52test_expect_success 'replace (case insensitive)' '
  53
  54test_hashmap "put key1 value1
  55put Key1 value2
  56put fooBarFrotz value3
  57put foobarfrotz value4
  58size" "NULL
  59value1
  60NULL
  61value3
  6264 2" ignorecase
  63
  64'
  65
  66test_expect_success 'get' '
  67
  68test_hashmap "put key1 value1
  69put key2 value2
  70put fooBarFrotz value3
  71put foobarfrotz value4
  72get key1
  73get key2
  74get fooBarFrotz
  75get notInMap" "NULL
  76NULL
  77NULL
  78NULL
  79value1
  80value2
  81value3
  82NULL"
  83
  84'
  85
  86test_expect_success 'get (case insensitive)' '
  87
  88test_hashmap "put key1 value1
  89put key2 value2
  90put fooBarFrotz value3
  91get Key1
  92get keY2
  93get foobarfrotz
  94get notInMap" "NULL
  95NULL
  96NULL
  97value1
  98value2
  99value3
 100NULL" ignorecase
 101
 102'
 103
 104test_expect_success 'add' '
 105
 106test_hashmap "add key1 value1
 107add key1 value2
 108add fooBarFrotz value3
 109add fooBarFrotz value4
 110get key1
 111get fooBarFrotz
 112get notInMap" "value2
 113value1
 114value4
 115value3
 116NULL"
 117
 118'
 119
 120test_expect_success 'add (case insensitive)' '
 121
 122test_hashmap "add key1 value1
 123add Key1 value2
 124add fooBarFrotz value3
 125add foobarfrotz value4
 126get key1
 127get Foobarfrotz
 128get notInMap" "value2
 129value1
 130value4
 131value3
 132NULL" ignorecase
 133
 134'
 135
 136test_expect_success 'remove' '
 137
 138test_hashmap "put key1 value1
 139put key2 value2
 140put fooBarFrotz value3
 141remove key1
 142remove key2
 143remove notInMap
 144size" "NULL
 145NULL
 146NULL
 147value1
 148value2
 149NULL
 15064 1"
 151
 152'
 153
 154test_expect_success 'remove (case insensitive)' '
 155
 156test_hashmap "put key1 value1
 157put key2 value2
 158put fooBarFrotz value3
 159remove Key1
 160remove keY2
 161remove notInMap
 162size" "NULL
 163NULL
 164NULL
 165value1
 166value2
 167NULL
 16864 1" ignorecase
 169
 170'
 171
 172test_expect_success 'iterate' '
 173        test-tool hashmap >actual.raw <<-\EOF &&
 174        put key1 value1
 175        put key2 value2
 176        put fooBarFrotz value3
 177        iterate
 178        EOF
 179
 180        cat >expect <<-\EOF &&
 181        NULL
 182        NULL
 183        NULL
 184        fooBarFrotz value3
 185        key1 value1
 186        key2 value2
 187        EOF
 188
 189        sort <actual.raw >actual &&
 190        test_cmp expect actual
 191'
 192
 193test_expect_success 'iterate (case insensitive)' '
 194        test-tool hashmap ignorecase >actual.raw <<-\EOF &&
 195        put key1 value1
 196        put key2 value2
 197        put fooBarFrotz value3
 198        iterate
 199        EOF
 200
 201        cat >expect <<-\EOF &&
 202        NULL
 203        NULL
 204        NULL
 205        fooBarFrotz value3
 206        key1 value1
 207        key2 value2
 208        EOF
 209
 210        sort <actual.raw >actual &&
 211        test_cmp expect actual
 212'
 213
 214test_expect_success 'grow / shrink' '
 215
 216        rm -f in &&
 217        rm -f expect &&
 218        for n in $(test_seq 51)
 219        do
 220                echo put key$n value$n >> in &&
 221                echo NULL >> expect
 222        done &&
 223        echo size >> in &&
 224        echo 64 51 >> expect &&
 225        echo put key52 value52 >> in &&
 226        echo NULL >> expect &&
 227        echo size >> in &&
 228        echo 256 52 >> expect &&
 229        for n in $(test_seq 12)
 230        do
 231                echo remove key$n >> in &&
 232                echo value$n >> expect
 233        done &&
 234        echo size >> in &&
 235        echo 256 40 >> expect &&
 236        echo remove key40 >> in &&
 237        echo value40 >> expect &&
 238        echo size >> in &&
 239        echo 64 39 >> expect &&
 240        cat in | test-tool hashmap > out &&
 241        test_cmp expect out
 242
 243'
 244
 245test_expect_success 'string interning' '
 246
 247test_hashmap "intern value1
 248intern Value1
 249intern value2
 250intern value2
 251" "value1
 252Value1
 253value2
 254value2"
 255
 256'
 257
 258test_done