Compile function Test_Memory_Services(@Test)
#pragma precomp SRP_PreCompiler

$insert LOGICAL

Declare function    Memory_Services
Declare subroutine  Memory_Services

// TODO: Code to run before each test goes here

GotoTest

// TODO: Code to run after each test goes here

Return TestResult or 1

//-----------------------------------------------------------------------------
// TESTS
//-----------------------------------------------------------------------------

Test DefaultHashTableHandle

    Handle1 = Memory_Services('CreateHashTable')
    Assert Handle1 NE ''

    Handle2 = Memory_Services('GetHandle')
    Assert Handle1 EQ Handle2

    Memory_Services('ReleaseHashTable')
    Handle3 = Memory_Services('GetHandle')
    Assert Handle3 equals ''

End Test


Test DefaultHashTableValue

    Memory_Services('CreateHashTable')

    Memory_Services('SetValue', 'Test', 'Test')
    Val     = Memory_Services('GetValue', 'Test')
    Assert Val equals 'Test'

    KeyExists   = Memory_Services('KeyExists', 'Test')
    Assert KeyExists equals True$

    Memory_Services('RemoveKey', 'Test')
    KeyExists   = Memory_Services('KeyExists', 'Test')
    Assert KeyExists equals False$

    Memory_Services('ReleaseHashTable')

End Test


Test DefaultHashTableValueCurrent

    Memory_Services('CreateHashTable')

    Memory_Services('SetValue', 'Test', 'Test')

    Current     = Memory_Services('IsValueCurrent', 'Test', 4)
    Assert Current equals True$

    Now = Time()
    Loop
    Until Time() - Now GE 3
    Repeat
    Current     = Memory_Services('IsValueCurrent', 'Test', 4, True$)
    Assert Current equals True$

    Now = Time()
    Loop
    Until Time() - Now GE 3
    Repeat
    Current     = Memory_Services('IsValueCurrent', 'Test', 4)
    Assert Current equals True$

    Now = Time()
    Loop
    Until Time() - Now GE 2
    Repeat
    Current     = Memory_Services('IsValueCurrent', 'Test', 4)
    Assert Current equals False$

    Memory_Services('ReleaseHashTable')

End Test


Test DefaultHashTableValueExpired

    Memory_Services('CreateHashTable')

    Memory_Services('SetValue', 'Test', 'Test')

    Expired     = Memory_Services('IsValueExpired', 'Test', 4)
    Assert Expired equals False$

    Now = Time()
    Loop
    Until Time() - Now GE 3
    Repeat
    Expired     = Memory_Services('IsValueExpired', 'Test', 4, True$)
    Assert Expired equals False$

    Now = Time()
    Loop
    Until Time() - Now GE 3
    Repeat
    Expired     = Memory_Services('IsValueExpired', 'Test', 4)
    Assert Expired equals False$

    Now = Time()
    Loop
    Until Time() - Now GE 2
    Repeat
    Expired     = Memory_Services('IsValueExpired', 'Test', 4)
    Assert Expired equals True$

    Memory_Services('ReleaseHashTable')

End Test


Test NamedHashTableHandle

    CacheName   = 'Name1'

    Handle1 = Memory_Services('CreateHashTable', CacheName)
    Assert Handle1 NE ''

    Handle2 = Memory_Services('GetHandle', CacheName)
    Assert Handle1 equals Handle2

    Memory_Services('ReleaseHashTable', CacheName)
    Handle3 = Memory_Services('GetHandle', CacheName)
    Assert Handle3 equals ''

End Test


Test NamedHashTableValue

    CacheName   = 'Name1'
    Memory_Services('CreateHashTable', CacheName)

    Memory_Services('SetValue', 'Test', 'Test', CacheName)
    Val     = Memory_Services('GetValue', 'Test', '', '', CacheName)
    Assert Val equals 'Test'

    KeyExists   = Memory_Services('KeyExists', 'Test', CacheName)
    Assert KeyExists equals True$

    Memory_Services('RemoveKey', 'Test', CacheName)
    KeyExists   = Memory_Services('KeyExists', 'Test', CacheName)
    Assert KeyExists equals False$

    Memory_Services('ReleaseHashTable', CacheName)

End Test


Test NamedHashTableValueCurrent

    CacheName   = 'Name1'
    Memory_Services('CreateHashTable', CacheName)

    Memory_Services('SetValue', 'Test', 'Test', CacheName)

    Current     = Memory_Services('IsValueCurrent', 'Test', 4, '', CacheName)
    Assert Current equals True$

    Now = Time()
    Loop
    Until Time() - Now GE 3
    Repeat
    Current     = Memory_Services('IsValueCurrent', 'Test', 4, True$, CacheName)
    Assert Current equals True$

    Now = Time()
    Loop
    Until Time() - Now GE 3
    Repeat
    Current     = Memory_Services('IsValueCurrent', 'Test', 4, '', CacheName)
    Assert Current equals True$

    Now = Time()
    Loop
    Until Time() - Now GE 2
    Repeat
    Current     = Memory_Services('IsValueCurrent', 'Test', 4, '', CacheName)
    Assert Current equals False$

    Memory_Services('ReleaseHashTable', CacheName)

End Test


Test NamedHashTableValueExpired

    CacheName   = 'Name1'
    Memory_Services('CreateHashTable', CacheName)

    Memory_Services('SetValue', 'Test', 'Test', CacheName)

    Expired     = Memory_Services('IsValueExpired', 'Test', 4, '', CacheName)
    Assert Expired equals False$

    Now = Time()
    Loop
    Until Time() - Now GE 3
    Repeat
    Expired     = Memory_Services('IsValueExpired', 'Test', 4, True$, CacheName)
    Assert Expired equals False$

    Now = Time()
    Loop
    Until Time() - Now GE 3
    Repeat
    Expired     = Memory_Services('IsValueExpired', 'Test', 4, '', CacheName)
    Assert Expired equals False$

    Now = Time()
    Loop
    Until Time() - Now GE 2
    Repeat
    Expired     = Memory_Services('IsValueExpired', 'Test', 4, '', CacheName)
    Assert Expired equals True$

    Memory_Services('ReleaseHashTable', CacheName)

End Test


Test MultipleHashTables

    CacheName   = 'Name1'

    Handle1 = Memory_Services('CreateHashTable')
    Assert Handle1 NE ''

    Handle2 = Memory_Services('CreateHashTable', CacheName)
    Assert Handle2 NE ''

    Assert Handle1 NE Handle2

    Memory_Services('SetValue', 'Test', 'Val1')
    Assert Memory_Services('GetValue', 'Test') equals 'Val1'

    Memory_Services('SetValue', 'Test', 'Val2', CacheName)
    Assert Memory_Services('GetValue', 'Test', '', '', CacheName) equals 'Val2'

    Memory_Services('ReleaseHashTable')
    Assert Memory_Services('GetHandle') equals ''
    Assert Memory_Services('GetHandle', CacheName) NE ''

    Memory_Services('ReleaseHashTable', CacheName)
    Assert Memory_Services('GetHandle', CacheName) equals ''

End Test


Test ReleaseAllHashTables

    CacheName   = 'Name1'

    Handle1 = Memory_Services('CreateHashTable')
    Assert Handle1 NE ''

    Handle2 = Memory_Services('CreateHashTable', CacheName)
    Assert Handle2 NE ''

    Assert Handle1 NE Handle2

    Memory_Services('ReleaseAllHashTables')
    Assert Memory_Services('GetHandle') equals ''
    Assert Memory_Services('GetHandle', CacheName) equals ''

End Test
