File: test_hash.pro

package info (click to toggle)
gnudatalanguage 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,832 kB
  • sloc: cpp: 198,435; ansic: 47,740; sh: 691; python: 474; makefile: 149; xml: 69; f90: 28
file content (185 lines) | stat: -rw-r--r-- 6,181 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
;
; basic tests on HASH() and related functionnalities
;
; -----------------------------------------------
; 
; Modifications history :
;
; - 2019-11-06 : AC :  rewrite header, code cleaning
;
; -----------------------------------------------
;
pro TEST_HASH, debug=debug, verbose=verbose, no_exit=no_exit
;
nb_errors = 0
;
; below a few simple tests on HASH before 
; the more internal tests provided originally
;
; define two hashtables, with pointers and check Where()
a=33L
p=PTR_NEW(a)
hsh = HASH('key1', 1.414, 'key2', 3.14, 'key3', ptr_new(a)) ;
hshp = HASH('key1', 1.414, 'key2', 3.14, 'key3', p) ;

c=hsh.where(PTR_NEW(a)) ; c must be a 0 element list  IS NOT AT THE MOMENT. Issued 
; SUPRESSED UNTIL issue  #578 has been closed.
;if ~c.IsEmpty() then ERRORS_ADD, nb_errors,' error where() on different pointers to same value '

c=hsh.where(1.414) ; c must be a LIST of 1 element and c[0]='key1'
if c ne 'key1' then ERRORS_ADD, nb_errors,' error where() on key=Float '

c=hshp.where(p) ; 
if c ne 'key3' then ERRORS_ADD, nb_errors,' error where() on key=ptr '

; now check EQ (I suppose NEQ will work)

result = hsh EQ 1.414
if result.count() ne 1 and result[0] ne 'key1' then ERRORS_ADD, nb_errors,' error EQ Float for HASH ' 

result = hshp EQ p
if result.count() ne 1 and result[0] ne 'key3' then ERRORS_ADD, nb_errors,' error EQ pointer for HASH ' 

; "more internal" tests - to de bedited and made more undertsandable to maintainers.
;
isgit = 0
DEFSYSV,"!GDL",exists=isgdl
if isgdl then isgit = STRPOS(!GDL.release,'git') gt 0

isgit = 0 ; no more excuses.

if isgit then MESSAGE, /cont,' GDL/git is detected so some tests will be excused,'

if (isgit and KEYWORD_SET(verbose)) then begin
  print,' Principally, those that traverse beyond a 1-Dimensional hash access'
  print,"   h = HASH('a', HASH('b', HASH('c', 5))) "
  print,'   we cannot access  '+" h['a', 'b', 'c'] = 5"
endif

; create hash1 as a foldcase hash, using all lowercase for keys.
hash1 = HASH('key1', 1, 'key2', 2, 'key3', 3, 'badpi', 3.14)
struchash = { key1: 1, key2: 2, key3: 3, badpi: 3.14}

if KEYWORD_SET(verbose) then begin
   print,' extract a struct into a hash:'
   print," hash1 = HASH('key1', 1, 'key2', 2, 'key3', 3, 'badpi', 3.14) & print,hash1 "
   print,hash1
   print,' struchash = { key1: 1, key2: 2, key3: 3, badpi: 3.14} & print,struchash '
   print,struchash
   help,/st,struchash
endif
; 
;; make a comparison hash from the structure.
;
hcomp = HASH(struchash,/lower,/fold)
nstash = N_TAGS(struchash)
txt=' structure was not properly stashed into the hash <hcomp = hash(struchash,/lower)> '
if (hcomp.count() ne nstash) then ERRORS_ADD, nb_errors, txt

hhtest =  hcomp eq hash1

if KEYWORD_SET(verbose) then begin
   print,' hcomp = hash(struchash,/lower)'
   help, hhtest & print, hhtest
endif

hcomp = HASH(struchash,/fold)

if KEYWORD_SET(verbose) then begin
   print,' hcomp = hash(struchash,/FOLD_CASE) & help, hcomp eq hash1 '
   hcomp = HASH(struchash,/FOLD_CASE)
   IF KEYWORD_SET(test) THEN BEGIN
   message,/cont,' exhibiting issue #702 ...'
   help, hcomp eq hash1	  ; after sucessful completion, causes interpreter to return to caller. 
ll=hhtest[1:2] & help,ll ; this will substitute fine.

stop ; (doesn't happen due to above "help, hcomp eq hash1")
	ENDIF
   print," keys = [ 'key1', 'key3' ] & print, hash1[keys] "
   keys = [ 'key1', 'key3' ]
   print, hash1[keys]
   MESSAGE, /continue,' End verbose block'
endif

if ~isgit then begin
endif
;
; COPY a hash:
;
if KEYWORD_SET(verbose) then $
   print," hash1 = HASH('key1', 1, 'key2', 2) & hash2 = hash1 & hash2['key1'] = 'hello' "
hash1 = HASH('key1', 1, 'key2', 2)
hash2 = hash1
hash2['key1'] = 'hello'

if KEYWORD_SET(verbose) then $
   print, " hash1['key1']: ", hash1['key1'], "   hash2['key1']: ", hash2['key1']
if( ~isgit) then begin
endif

keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
values = LIST('one', 2.0, 3, 4l, PTR_NEW(5), {n:6}, COMPLEX(7,0))
htest = HASH(keys, values)
IF N_ELEMENTS(htest) ne 7 then $
    ERRORS_ADD, nb_errors,$
    ' N_ELEMENTS(htest) ne 7  .. fail '

; Tostruct(/recursive)
   struct = {FIELD1: 4.0, FIELD2: {SUBFIELD1: "hello", SUBFIELD2: 3.14, subfield3: 6.28}}
   hash = HASH(struct, /EXTRACT)
   sback = hash.toStruct(/recursive)
   if ~ISA(sback.FIELD2,'STRUCT')  then begin
      ERRORS_ADD, nb_errors
      MESSAGE,/cont,  ' HASH.ToStruct(/recursive)  failed'
   endif else begin
      if KEYWORD_SET(verbose) and ~isgit then begin
         message,/cont, ' HASH.ToStruct(/recursive)  succeeded'
      endif
   endelse
   ;;
   keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
   scalars=hash(keys) ;test with no key
   scalars=hash(keys,0)
   eq7 = scalars.count(0)
   if isgit then scalars[keys[1:4]] = 4+intarr(4) else $
      scalars[keys[1:4]] = 4
   eq4 = scalars.count(4)

; git should be able to do HasKey()
hbw = HASH('black', 0, 'gray', 128, 'grey', 128, 'white', 255)

if KEYWORD_SET(verbose) then $
   print,[ hbw.HasKey('gray'), hbw.HasKey(['grey','red','white'])]

keys = ['a','b','c','d','e','f','g']

scalars=HASH(keys,intarr(n_elements(keys)))
scalars[keys]=100+INDGEN(n_elements(keys))

if KEYWORD_SET(verbose) then begin
   foreach value, scalars, key do print," key:",key," =",value
   print,'scalars[keys]=100+indgen(n_elements(keys))'
endif

more = ['h','j','k']
scalars += hash(more,more)
if KEYWORD_SET(verbose) then $
    foreach value, scalars[more], key do print," key:",key," =",value
allkeys= scalars.keys()
scalars[allkeys]=allkeys.toarray()
if KEYWORD_SET(verbose) then $
    foreach value, scalars, key do print," key:",key," =",value
;
; ----------------- final messages ----------
;
BANNER_FOR_TESTSUITE, 'TEST_HASH', nb_errors, short=short
;
if KEYWORD_SET(test) then STOP, 'keyword TEST set: stop'
;
if (nb_errors GT 0) AND ~KEYWORD_SET(no_exit) then EXIT, status=1
;
end
;    ERRORS_ADD, nb_errors,$
;        ' eq7 = scalars.count(0) is not 7'
;if eq4 ne 4 then    ERRORS_ADD, nb_errors,$
;        ' scalars[keys[1:4]] = 4 scalars.count(4) is not 4'