File: cachetest.py

package info (click to toggle)
okasha 0.2.4-4
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye, sid
  • size: 856 kB
  • sloc: python: 1,092; sh: 29; makefile: 7
file content (76 lines) | stat: -rw-r--r-- 2,260 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys, os, os.path, time

print() 

try:
  from okasha.utils import ObjectsCache
except ImportError:
  try: 
    sys.path.insert(0,os.path.abspath(os.curdir))
    from okasha.utils import ObjectsCache
  except ImportError:
    sys.path.insert(0,os.path.abspath(os.path.join(os.curdir,'..')))
    from okasha.utils import ObjectsCache

ch=ObjectsCache(lock=None, minCount=10, maxCount=100, maxTime=0)
print("asserting empty start: ", end=' ')
assert(len(ch.objs)==0)
print("OK")

print("asserting respecting minCount: ", end=' ')
for i in range(10):
  ch.append(i,i)
  assert(len(ch.objs)==i+1)
print("OK, after inserting 10 objects we have ", len(ch.objs), " cached")

print("asserting getting least expected while below minCount: ", end=' ')
ch=ObjectsCache(lock=None, minCount=10, maxCount=100, maxTime=0)
for i in range(10):
  ch.append(i,100-3*i)
  for j in range(i+1):
    assert(ch.get(j)==100-3*j)
print("OK")


print("asserting getting while below minCount: ", end=' ')
ch=ObjectsCache(lock=None, minCount=10, maxCount=100, maxTime=0)
for i in range(10):
  ch.append(i,100-3*i)
  for j in range(i,-1,-1):
    assert(ch.get(j)==100-3*j)
print("OK")

print("asserting below maxCount: ", end=' ')
ch=ObjectsCache(lock=None, minCount=10, maxCount=100, maxTime=0)
for i in range(100):
  ch.append(i,3*i)
  for j in range(i+1):
    assert(ch.get(j)==3*j)
print("OK")

print("asserting above maxCount (132 objs in 100 max): ", end=' ')
ch=ObjectsCache(lock=None, minCount=10, maxCount=100, maxTime=0)
maxmissing=0
for i in range(132):
  #print "inserting id=", i,", value=",3*i
  ch.append(i,3*i)
  missing=0
  for j in range(i+1):
    #print "** j=",j,", ids=", map(lambda o:o.objId, ch.objs),", objs=",map(lambda o:o.obj, ch.objs)
    v=ch.get(j)
    assert(v==3*j or v==None)
    if v==None: missing+=1
  maxmissing=max(missing,maxmissing)
assert(maxmissing<=32)
print("OK, we are keeping ", len(ch.objs), " objects and the maximum missing=",maxmissing)


import random
random.seed(time.time())
print("random testing: ")
# insert 5 values then choose randomly between getting or inserting
# when in getting mode, choose a random number of how many times to repeat getting the same id

#ch.append(objId, obj)