logcount.lsp 1.23 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Thu Sep 11 23:12:56 2003
;;;; Contains: Tests of LOGCOUNT

6

7

8 9
;;; Error tests

10
(deftest logcount.error.1
11 12
  (signals-error (logcount) program-error)
  t)
13 14

(deftest logcount.error.2
15 16
  (signals-error (logcount 0 nil) program-error)
  t)
17 18

(deftest logcount.error.3
pfdietz's avatar
pfdietz committed
19
  (check-type-error #'logcount #'integerp)
20 21
  nil)

22 23
;;; Non-error tests

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
(deftest logcount.1
  (logcount 0)
  0)

(deftest logcount.2
  (logcount 1)
  1)

(deftest logcount.3
  (logcount 2)
  1)

(deftest logcount.4
  (logcount 3)
  2)

(deftest logcount.5
  (logcount -1)
  0)

(deftest logcount.6
  (loop for x = (random-fixnum)
sds's avatar
sds committed
46 47
        repeat 100
        always (eql (logcount x) (logcount (lognot x))))
48 49 50 51 52
  t)

(deftest logcount.7
  (let ((bound (ash 1 300)))
    (loop for x = (random-from-interval bound)
sds's avatar
sds committed
53 54
          repeat 100
          always (eql (logcount x) (logcount (lognot x)))))
55 56 57 58
  t)

(deftest logcount.8
  (loop for y = (random (1+ most-positive-fixnum))
sds's avatar
sds committed
59 60 61 62 63 64 65 66 67 68
        repeat 100
        unless
        (let ((cnt 0)
              (x y))
          (loop while (> x 0)
                do
                (when (oddp x) (incf cnt))
                (setf x (ash x -1)))
          (eql cnt (logcount y)))
        collect y)
69 70 71
  nil)