text
stringlengths
0
15.7k
source
stringlengths
6
112
end assert test failed
lns3.scpt
to unit test runner
lns3.scpt
try -- shouldn't fail unless TestTools library has been incorrectly built
lns3.scpt
return (path to resource "osatest" in directory "bin") as «class furl»
lns3.scpt
on error number -2763 -- returned 'nothing' instead of raising 'resource not found' error (very odd)
lns3.scpt
error "A resource wasn’t found." number -192
lns3.scpt
_error("unit test runner", eText, eNumber, eFrom, eTo)
lns3.scpt
end unit test runner
lns3.scpt
on run unit tests testFile -- e.g. for running tests in Script Editor
lns3.scpt
return do shell script (quoted form of POSIX path of (unit test runner)) & space ¬
lns3.scpt
& (quoted form of _support's asPOSIXPathParameter(testFile, "")) without altering line endings
lns3.scpt
_error("run unit tests", eText, eNumber, eFrom, eTo)
lns3.scpt
end run unit tests
lns3.scpt
_support's throwCommandError("Objects", handlerName, eText, eNumber, eFrom, eTo)
lns3.scpt
to _makeEmptyList(n) -- make a list of size n where n is a power of two, e.g. _makeBucketList(1024) returns a 1024 (2^10) item list (other values of n are rounded down automatically, e.g. n=1000 gives a 512-item list)
lns3.scpt
set l to {missing value}
lns3.scpt
repeat while n > 1
lns3.scpt
set l to l & l
lns3.scpt
set n to n div 2
lns3.scpt
return l
lns3.scpt
end _makeEmptyList
lns3.scpt
to dictionary collection -- key-value collection, analogous to NSMutableDictionary (note: keys must be text, or objects that reliably coerce to text)
lns3.scpt
script DictionaryCollection
lns3.scpt
property _buckets : _makeEmptyList(1024) -- DictionaryCollections currently use a fixed-size bucket list (2^10=1024 items seems to be a reasonable default)
lns3.scpt
property _size : length of my _buckets
lns3.scpt
property _count : 0
lns3.scpt
_support's throwMethodError("Objects", "DictionaryCollection", handlerName, eText, eNumber, eFrom, eTo)
lns3.scpt
to _keyIndex(theKey)
lns3.scpt
if (count {theKey} each specifier) ≠ 0 or (count {theKey} each record) ≠ 0 or (count {theKey} each script) ≠ 0 then error "Invalid key type." number -1703 from theKey -- explicitly disallow types that can redefine their `class` properties
lns3.scpt
if {theKey's class} is in {type, class, constant} then -- caution: coercing a type class or constant symbol to text is problematic, as the resulting string may contain either its literal name or its raw four-char code depending on the terminology available at the time (furthermore, terminology conflicts and synonyms may cause its literal name to change); to avoid these problems, it's better to send the value over the ASOC to convert it to an NSAppleEventDescriptor, from which its raw four-char code can be obtained as an Int32 which can be safely used as its hashNum
lns3.scpt
set hashNum to _support's asNSObject(theKey)'s typeCodeValue()
lns3.scpt
if theKey's class is text then
lns3.scpt
set hashNum to _support's asNormalizedNSString(theKey as text)'s hash()
lns3.scpt
else if {theKey's class} is in {integer, real} then -- note: integers and reals that are numerically equal must always produce same hash, since the linked list lookup compares integer and real keys for numeric equality (e.g. 1 = 1.0)
lns3.scpt
set hashNum to theKey
lns3.scpt
set divNum to theKey mod 1
lns3.scpt
if divNum ≠ 0 then set hashNum to hashNum - 1 / divNum
lns3.scpt
if hashNum < 0 then set hashNum to -hashNum
lns3.scpt
if hashNum < 1.0E+200 then set hashNum to hashNum * 67128023
lns3.scpt
else if theKey's class is date then
lns3.scpt
set hashNum to _support's asNSObject(theKey)'s timeIntervalSinceReferenceDate()
lns3.scpt
error "Invalid key type." number -1703 from theKey
lns3.scpt
if hashNum > 1.12589990684262E+15 then set hashNum to hashNum div 4096
lns3.scpt
return hashNum mod _size + 1
lns3.scpt
end _keyIndex
lns3.scpt
to countItems()
lns3.scpt
return _count
lns3.scpt
end countItems
lns3.scpt
to addItem(theKey, theValue)
lns3.scpt
set keyIndex to _keyIndex(theKey)
lns3.scpt
copy theKey to theKey -- while dates can be used as keys, it's essential that keys stored in linked lists are never mutated so they must be copied first (this is a no-op for text, numbers, and other immutable types, while other types for which copying is problematic, e.g. script objects, are already forbidden as keys)
lns3.scpt
set foundItem to item keyIndex of my _buckets
lns3.scpt
if foundItem is missing value then -- insert new item
lns3.scpt
set item keyIndex of my _buckets to {k:theKey, v:theValue, nextItem:missing value}
lns3.scpt
set _count to _count + 1
lns3.scpt
if k of foundItem is theKey then -- replace existing item
lns3.scpt
set oldValue to v of foundItem
lns3.scpt
set {k of foundItem, v of foundItem} to {theKey, theValue}
lns3.scpt
if nextItem of foundItem is missing value then -- insert new item
lns3.scpt
set nextItem of foundItem to {k:theKey, v:theValue, nextItem:missing value}
lns3.scpt
set foundItem to nextItem of foundItem
lns3.scpt
_error("addItem", eText, eNumber, eFrom, eTo)
lns3.scpt
end addItem
lns3.scpt
to removeItem(theKey)
lns3.scpt
set prevItem to missing value
lns3.scpt
repeat while foundItem is not missing value
lns3.scpt
set foundKey to k of foundItem
lns3.scpt
if foundKey is theKey then
lns3.scpt
set theValue to v of foundItem
lns3.scpt
if prevItem is missing value then
lns3.scpt
set item keyIndex of my _buckets to nextItem of foundItem
lns3.scpt
set nextItem of prevItem to nextItem of foundItem
lns3.scpt
set _count to _count - 1
lns3.scpt
return theValue -- return the removed value
lns3.scpt
set prevItem to foundItem
lns3.scpt
error "Item not found." number -1728 from theKey
lns3.scpt
_error("removeItem", eText, eNumber, eFrom, eTo)
lns3.scpt
end removeItem
lns3.scpt
to deleteAllItems()
lns3.scpt
set my _buckets to _makeEmptyList(1024)
lns3.scpt
set _size to length of my _buckets
lns3.scpt
set _count to 0
lns3.scpt
end deleteAllItems
lns3.scpt
to getItem(theKey)
lns3.scpt
set foundItem to item _keyIndex(theKey) of my _buckets
lns3.scpt
if k of foundItem is theKey then return v of foundItem
lns3.scpt
_error("getItem", eText, eNumber, eFrom, eTo)
lns3.scpt
end getItem
lns3.scpt
to containsItem(theKey)
lns3.scpt
getItem(theKey)
lns3.scpt
end containsItem
lns3.scpt
to objectDescription()
lns3.scpt
return "«dictionary collection (" & _count & " items)»"
lns3.scpt
end objectDescription
lns3.scpt
to copyObject()
lns3.scpt
set newObject to dictionary collection
lns3.scpt
if _count > 0 then
lns3.scpt
if length of my _buckets ≠ length of newObject's _buckets then error "Mismatched bucket lists." -- sanity check as this copyObject() implementation requires all bucket lists to be same length, and will need revised if growable bucket lists are supported in future
lns3.scpt
repeat with i from 1 to length of my _buckets
lns3.scpt
set oldBucket to item i of my _buckets
lns3.scpt