Skip to content

Instantly share code, notes, and snippets.

@avitale
Last active August 29, 2015 14:04
Show Gist options
  • Save avitale/e8bd84f6293f595a3845 to your computer and use it in GitHub Desktop.
Save avitale/e8bd84f6293f595a3845 to your computer and use it in GitHub Desktop.
Test for totalsizeof
using Base.Test
@test totalsizeof(4) == sizeof(4)
@test totalsizeof(4.0) == sizeof(4.0)
@test totalsizeof("Test") == sizeof("Test")
@test totalsizeof('\t') == sizeof('\t')
@test totalsizeof(Nothing) == sizeof(Nothing)
# Array of Integer
a = [1,2,3]
@test totalsizeof(a) == sizeof(a) + mapreduce(sizeof, +, a)
# Array of Float
a = [1,2.0,3]
@test totalsizeof(a) == sizeof(a) + mapreduce(sizeof, +, a)
# Array of Any
a = ["1",2,3]
@test totalsizeof(a) == sizeof(a) + mapreduce(sizeof, +, a)
# Tuples
a = (1, "Test")
@test totalsizeof(a) == sizeof(a[1]) + sizeof(a[2])
# No double counting
a = [1,2,3]
@test totalsizeof((a,a)) == totalsizeof(a)
# Dict
a = {"key" => "val"}
@test totalsizeof(a) == sizeof(a) + totalsizeof(a.slots) + sizeof(a.keys) + sizeof("key") + sizeof(a.vals) + sizeof("val") + totalsizeof(a.ndel) + totalsizeof(a.count) + totalsizeof(a.deleter)
# Handles circular references
a = {"key" => "val"}
a["b"] = a
@test totalsizeof(a) == sizeof(a) + totalsizeof(a.slots) + sizeof(a.keys) + sizeof("key") + sizeof("b") + sizeof(a.vals) + sizeof("val") + totalsizeof(a.ndel) + totalsizeof(a.count) + totalsizeof(a.deleter)
# Handles Array with undefined elements
a = Array(Any,5)
a[2] = 5
@test totalsizeof(a) == sizeof(a) + sizeof(a[2])
# Functions
@test totalsizeof(sizeof) == sizeof(sizeof)
# Types without sizeof have totalsizeof == 0
for i in [:Test, Integer]
@test totalsizeof(i) == 0
end
# Composite Types
a = sprand(5,10,.1)
# Note: avoid double counting immutable types
@test totalsizeof(a) == sizeof(a) + sizeof(a.colptr) + sizeof(a.rowval) + totalsizeof(a.nzval) + sizeof(1) * length(unique(vcat([a.m, a.n], a.colptr, a.rowval)))
# Doesn't throw when calculating Main
@test isa(totalsizeof(Main), Integer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment