1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl options.t'
3
4#########################
5
6# change 'tests => 1' to 'tests => last_test_to_print';
7
8use Test::More;
9BEGIN { use_ok('Sane') };
10
11#########################
12
13# Insert your test code below, the Test::More module is use()ed here so read
14# its man page ( perldoc Test::More ) for help writing this test script.
15
16plan skip_all => 'libsane 1.0.19 or better required'
17     unless Sane->get_version_scalar > 1.000018;
18
19my @array = Sane->get_version;
20is ($#array, 2, 'get_version');
21
22@array = Sane->get_devices;
23cmp_ok($Sane::STATUS, '==', SANE_STATUS_GOOD, 'get_devices');
24
25my $test = Sane::Device->open('test');
26cmp_ok($Sane::STATUS, '==', SANE_STATUS_GOOD, 'open');
27
28my $n = $test->get_option(0);
29cmp_ok($Sane::STATUS, '==', SANE_STATUS_GOOD, 'get number of options');
30
31my $options = $test->get_option_descriptor(21);
32if ($options->{name} eq 'enable-test-options') {
33 $info = $test->set_option(21, SANE_TRUE);
34 cmp_ok($Sane::STATUS, '==', SANE_STATUS_GOOD, 'enable-test-options');
35
36 for (my $i = 0; $i < $n; $i++) {
37  my $options = $test->get_option_descriptor($i);
38  isnt ($options, undef, 'get_option_descriptor');
39
40  if ($options->{cap} & SANE_CAP_SOFT_SELECT) {
41   my $in;
42   if ($options->{constraint_type} == SANE_CONSTRAINT_RANGE) {
43    if ($options->{max_values} == 1) {
44     $in = $options->{constraint}{min};
45    }
46    else {
47     for (my $i = 0; $i < $options->{max_values}; $i++) {
48      $in->[$i] = $options->{constraint}{min};
49     }
50    }
51   }
52   elsif ($options->{constraint_type} == SANE_CONSTRAINT_STRING_LIST or
53                     $options->{constraint_type} == SANE_CONSTRAINT_WORD_LIST) {
54    if ($options->{max_values} == 1) {
55     $in = $options->{constraint}[0];
56    }
57    else {
58     for (my $i = 0; $i < $options->{max_values}; $i++) {
59      $in->[$i] = $options->{constraint}[0];
60     }
61    }
62   }
63   elsif ($options->{type} == SANE_TYPE_BOOL or
64                                         $options->{type} == SANE_TYPE_BUTTON) {
65    $in = SANE_TRUE;
66   }
67   elsif ($options->{type} == SANE_TYPE_STRING) {
68    $in = 'this is a string with no constraint';
69   }
70   elsif ($options->{type} == SANE_TYPE_INT) {
71    if ($options->{max_values} == 1) {
72     $in = 12345678;
73    }
74    else {
75     for (my $i = 0; $i < $options->{max_values}; $i++) {
76      $in->[$i] = 12345678;
77     }
78    }
79   }
80   elsif ($options->{type} == SANE_TYPE_FIXED) {
81    if ($options->{max_values} == 1) {
82     $in = 1234.5678;
83    }
84    else {
85     for (my $i = 0; $i < $options->{max_values}; $i++) {
86      $in->[$i] = 1234.5678;
87     }
88    }
89   }
90   if (defined $in) {
91    SKIP: {
92     skip 'Pressing buttons produces too much output', 1 if $options->{type} == SANE_TYPE_BUTTON;
93
94     $info = $test->set_option($i, $in);
95    };
96    if ($options->{cap} & SANE_CAP_INACTIVE) {
97     cmp_ok($Sane::STATUS, '==', SANE_STATUS_INVAL, 'set_option');
98    }
99    else {
100     cmp_ok($Sane::STATUS, '==', SANE_STATUS_GOOD, 'set_option');
101    }
102
103    if ($options->{type} != SANE_TYPE_BUTTON) {
104     $out = $test->get_option($i);
105     if ($options->{cap} & SANE_CAP_INACTIVE) {
106      cmp_ok($Sane::STATUS, '==', SANE_STATUS_INVAL, 'get_option');
107     }
108     elsif ($info & SANE_INFO_INEXACT) {
109      cmp_ok($Sane::STATUS, '==', SANE_STATUS_GOOD, 'get_option');
110     }
111     elsif ($options->{type} == SANE_TYPE_FIXED) {
112      if ($in == 0) {
113       is (abs($out) < 1.e-6, 1, 'get_option');
114      }
115      else {
116       is (abs($out-$in)/$in < 1.e-6, 1, 'get_option');
117      }
118     }
119     else {
120      is_deeply ($out, $in, 'get_option');
121     }
122    }
123   }
124  }
125  if ($options->{cap} & SANE_CAP_AUTOMATIC and not $options->{cap} & SANE_CAP_INACTIVE) {
126   $info = $test->set_auto($i);
127   cmp_ok($Sane::STATUS, '==', SANE_STATUS_GOOD, 'set_auto');
128  }
129 }
130}
131
132done_testing();
133