1;;; memory-report-tests.el --- tests for memory-report.el -*- lexical-binding: t -*-
2
3;; Copyright (C) 2020-2021 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software: you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
19
20;;; Code:
21
22(require 'ert)
23(require 'memory-report)
24
25(defun setup-memory-report-tests ()
26  ;; Set the sizes on things based on a 64-bit architecture.  (We're
27  ;; hard-coding this to be able to write simple tests that'll work on
28  ;; all architectures.)
29  (memory-report--set-size
30   '((conses 16 499173 99889)
31     (symbols 48 22244 3)
32     (strings 32 92719 4559)
33     (string-bytes 1 40402011)
34     (vectors 16 31919)
35     (vector-slots 8 385148 149240)
36     (floats 8 434 4519)
37     (intervals 56 24499 997)
38     (buffers 984 33))))
39
40(ert-deftest memory-report-sizes ()
41  (setup-memory-report-tests)
42  (should (equal (memory-report-object-size (cons nil nil)) 16))
43  (should (equal (memory-report-object-size (cons 1 2)) 16))
44
45  (should (equal (memory-report-object-size (list 1 2)) 32))
46  (should (equal (memory-report-object-size (list 1)) 16))
47
48  (should (equal (memory-report-object-size (list 'foo)) 16))
49
50  (should (equal (memory-report-object-size (vector 1 2 3)) 64))
51  (should (equal (memory-report-object-size (vector 1 2 3 4)) 80))
52
53  (should (equal (memory-report-object-size "") 32))
54  (should (equal (memory-report-object-size "a") 33))
55  (should (equal (memory-report-object-size (propertize "a" 'face 'foo))
56                 81)))
57
58(ert-deftest memory-report-sizes-vectors ()
59  (should (= (memory-report--object-size
60              (make-hash-table :test #'eq)
61              ["long string that should be at least 40 bytes"])
62             108))
63  (let ((string "long string that should be at least 40 bytes"))
64    (should (= (memory-report--object-size
65                (make-hash-table :test #'eq)
66                (vector string))
67               108))
68    (should (= (memory-report--object-size
69                (make-hash-table :test #'eq)
70                (vector string string))
71               124))))
72
73(ert-deftest memory-report-sizes-structs ()
74  (cl-defstruct memory-report-test-struct
75    (item0 nil)
76    (item1 nil))
77  (let ((s (make-memory-report-test-struct :item0 "hello" :item1 "world")))
78    (should (= (memory-report-object-size s)
79               90))))
80
81(provide 'memory-report-tests)
82
83;;; memory-report-tests.el ends here
84