Skip to content

Commit

Permalink
added tests, updated constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
isc-rsaptars committed Oct 2, 2023
1 parent e5642cc commit 74a4fac
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 35 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.vscode/
.gitattributes
*.code-workspace
*.code-workspace

# using TestClass.cls for objecscript functionality exploration / convince myself that things work the way I expect them to
cls/SourceControl/Git/TestClass.cls
2 changes: 0 additions & 2 deletions cls/SourceControl/Git/.gitignore

This file was deleted.

50 changes: 18 additions & 32 deletions cls/SourceControl/Git/Util/PrivateMemoryStore.cls
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ Property offset [ InitialExpression = 0, Internal, Private ];

Property size [ Internal, Private ];

Property defaultSize [ InitialExpression = 128, Internal, Private ];
Parameter defaultSize = 128;

Method %OnNew(size As %Integer) As %Status [ Private, ServerOnly = 1 ]
Method %OnNew(size) As %Status [ Private, ServerOnly = 1 ]
{
if size <= 0 {
set size = i%defaultSize
if $DATA(size) && $ISVALIDNUM(size) && (size >= 0) {
set i%size = size
} else {
set i%size = ..#defaultSize
}
set i%size = size
set i%buffer = $zu(106,1,size)
w !, "Size set to ", i%size, !
set i%buffer = $zu(106,1,i%size)
quit $$$OK
}

Expand All @@ -27,18 +29,20 @@ Method Store(key, value)
set length = $length(value)
// this will clear it if it exists
do ..Clear(key)

if (length + i%offset > i%size) {
set requiredSize = length + i%offset
if (requiredSize > i%size) {
// TODO: there is definitely a better way to find the appropriate next size
// using log but won't do that right now
// using log_2() but won't do that right now

if i%size=0 {
set newSize = i%defaultSize
} else {
set newSize = i%size*2
}
do {

while requiredSize > newSize {
set newSize = newSize*2
} while (length + i%offset > newSize)
}
set newBuffer = $zu(106,1,newSize)

// move values from buffer to newBuffer
Expand All @@ -53,7 +57,7 @@ Method Store(key, value)
set i%offset = newOffset
merge i%map = newMap
}
// add mapping for the
// add mapping for the key
set i%map(key) = $lb(i%offset,length)
set i%offset = ..insertIntoMemoryStore(value, i%buffer, i%offset)
}
Expand Down Expand Up @@ -89,27 +93,10 @@ Method KeyExists(key) As %Boolean
return '($Get(i%map(key)) = "")
}

ClassMethod Test()
{
set inst = ..%New(-2)
do inst.Store("foo","bar")
write !,"foo: ",inst.Retrieve("foo")
do inst.Store("foo2","Hello World!")
write !,"foo: ",inst.Retrieve("foo")
write !,"foo2: ",inst.Retrieve("foo2")
zw inst
break
do inst.Store("foo2","Bye World!")
do inst.Clear("foo")
do inst.Store("foo3","This is actually quite fun")
write !,"foo: ",inst.Retrieve("foo")
write !,"foo2: ",inst.Retrieve("foo2")
write !,"foo3: ",inst.Retrieve("foo3"), !
zw inst
}

// PRIVATE METHODS ====>

// Writes to Buffer and returns new offset

Method insertIntoMemoryStore(value, buffer, offset) As %Integer [ Private ]
{
set length = $length(value)
Expand Down Expand Up @@ -149,7 +136,6 @@ Method compactBuffer(buffer, Output newMap, Output newOffset) [ Private ]

Method deallocateBuffer() [ Private ]
{
w !, "deallocating ", i%buffer, " of size ", i%size, !
do ..clearBuffer()
set i%size = 0
kill i%map
Expand Down
1 change: 1 addition & 0 deletions module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<!-- Unit tests -->
<UnitTest Name="/test" Package="UnitTest.SourceControl.Git" />
<UnitTest Name="/testUtils" Package="UnitTest.SourceControl.Git.Utils" />

<!-- If building in developer mode, this will build the web UI. -->
<Invoke Class="SourceControl.Git.Build" Method="BuildUIForDevMode" Phase="Compile">
Expand Down
49 changes: 49 additions & 0 deletions test/UnitTest/SourceControl/Git/PrivateMemoryStore.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Class UnitTest.SourceControl.Git.PrivateMemoryStore Extends %UnitTest.TestCase
{

Method TestInsert()
{
set pvtMemStore = ##class(SourceControl.Git.Util.PrivateMemoryStore).%New(16)
set keys = $lb("a", "b", "c")
set values = $lb("123456", "789101112", "131415161718")
set n = $ListLength(keys)
do ..InsertToMemoryStore(pvtMemStore, keys, values)

for i=1:1:n {
Set key = $List(keys,i)
Set expectedValue = $List(values,i)

do $$$AssertEquals(pvtMemStore.Retrieve(key), expectedValue)
}
}

ClassMethod InsertToMemoryStore(store, keys, ByRef values)
{
set n = $ListLength(keys)
for i=1:1:n {
Set key = $List(keys,i)
Set value = $List(values,i)
do store.Store(key, value)
}
}

Method TestDelete()
{
set pvtMemStore = ##class(SourceControl.Git.Util.PrivateMemoryStore).%New(16)
set keys = $lb("a", "b", "c")
set values = $lb("123456", "789101112", "131415161718")
do ..InsertToMemoryStore(pvtMemStore, keys, values)
set n = $ListLength(keys)

for i=1:1:n-1 {
do pvtMemStore.Clear($List(keys,i))
}

for i=1:1:n-1 {
do $$$AssertEquals(pvtMemStore.KeyExists($List(keys,i)), 0)
}

do $$$AssertEquals(pvtMemStore.Retrieve($List(keys,n)), $List(values,n))
}

}
26 changes: 26 additions & 0 deletions test/UnitTest/SourceControl/Git/PrivateMemoryStoreTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Import SourceControl.Git.Util

Class test.UnitTest.SourceControl.Git.Util.PrivateMemoryStoreTest Extends %UnitTest.TestCase
{

Method TestInsert()
{
set pvtMemStore = ##class(PrivateMemoryStore).%New(16)
set keys = $lb("a", "b", "c")
set values = $lb("123456", "789101112", "131415161718")
set n = $ListLength(keys)
for i=1:1:n {
Set key = $List(keys,i)
Set value = $List(values,i)

do pvtMemStore.Store(key, value)
}
for i=1:1:n {
Set key = $List(keys,i)
Set expectedValue = $List(values,i)

do $$$AssertEquals(pvtMemStore.Retrieve(key), expectedValue)
}
}

}

0 comments on commit 74a4fac

Please sign in to comment.