about summary refs log tree commit diff
path: root/test/pbmnoise2.test
blob: 8845e3b88fbdddf30860bddd1dd0d456d409bbb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /bin/sh
# This script tests: pbmnoise
# Also requires: pgmhist

# Expected value and variance of binominal distribution:
# Pb: probability of getting a black pixel (=ratio)
# Pw: probability of getting a white pixel (= 1 - Pb = 1 - ratio)
#
# E(width, height, Pb, Pw) = width * height * Pb
# V(width, height, Pb, Pw) = width * height * Pb * Pw
#
# When width * height is large and Pb is not too near edges
# approximate with normal distribution

echo "Test 1.  Should print OK 1, then OK 2 three times"

args1="-endian=big -randomseed=1"

( pbmnoise            ${args1} 320 200 | pgmhist
  pbmnoise -ratio=1/2 ${args1} 160 400 | pgmhist
  pbmnoise -ratio=2/4 ${args1} 64000 1 | pgmhist
  pbmnoise -pack      ${args1} 10 6400 | pgmhist ) | \
  awk 'BEGIN { pixels=64000; Pb=Pw=0.5;
               average=pixels*Pb;
               sd=sqrt(pixels*Pb*Pw);  # standard deviation
               sl=1.96                 # significance level 
               limit1 = average - sd * sl;
               limit2 = average + sd * sl
               count=-1  # initial value
              }
       $1!=0  { next }
              { v=1  }    # initial value
       limit1 < $2 && $2 < limit2 { v=0 }
       v==0   {print "OK", (count==prev)+1}
       v==1   {print "NG" $0}
              {prev=count}'



echo "Test 2.  Should print OK 1, then OK 2 three times"

args2="-endian=big -randomseed=2"

( pbmnoise -ratio=11/32        ${args1} 32  119 | pgmhist
  pbmnoise -ratio=11/32        ${args1} 224  17 | pgmhist
  pbmnoise -ratio=22/64        ${args1} 3808  1 | pgmhist
  pbmnoise -ratio=44/128 -pack ${args1} 1904 2 | pgmhist ) | \
  awk 'BEGIN {pixels=32*119; Pb=11/32; Pw=1-Pb;
               average=pixels*Pb;
               sd=sqrt(pixels*Pb*Pw);  # standard deviation
               sl=1.96                 # significance level 
               limit1 = average - sd * sl;
               limit2 = average + sd * sl
               count=-1  # initial value
              }
       $1!=0  { next }
              { v=1  }    # initial value
       limit1 < $2 && $2 < limit2 { v=0 }
       v==0   {print "OK", (count==prev)+1}
       v==1   {print "NG" $0}
              {prev=count}'




echo "Test 3-black.  Should print 255    384    100%    100% four times"

pbmnoise -ratio=0/1     -randomseed=1       128 3 | pgmhist | \
  awk '$1==0 || $1==255'
pbmnoise -ratio=0/1024  -randomseed=1       64  6 | pgmhist | \
  awk '$1==0 || $1==255'
pbmnoise -ratio=0/8192  -randomseed=1       32 12 | pgmhist | \
  awk '$1==0 || $1==255'
pbmnoise -ratio=0/65536 -randomseed=1 -pack 1 384 | pgmhist | \
  awk '$1==0 || $1==255'

echo "Test 3-white.  Should print 0    320    100%    100% three times"

pbmnoise -ratio=1/1         -randomseed=1       64  5 | pgmhist | \
  awk '$1==0 || $1==255'
pbmnoise -ratio=8/8         -randomseed=1       32 10 | pgmhist | \
  awk '$1==0 || $1==255'
pbmnoise -ratio=65536/65536 -randomseed=1 -pack 2 160 | pgmhist | \
  awk '$1==0 || $1==255'