Skip to content
This repository was archived by the owner on Jul 6, 2019. It is now read-only.

Commit a657e06

Browse files
committed
Merge pull request #128 from bgamari/ioreg-test
Add rudimentary tests for ioreg
2 parents cd90861 + 81119a8 commit a657e06

File tree

7 files changed

+190
-39
lines changed

7 files changed

+190
-39
lines changed

Rakefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Context.create(__FILE__, ENV['PLATFORM'])
77

88
provide_stdlibs
99

10+
# shiny
11+
compile_rust :shiny_crate, {
12+
source: 'thirdparty/shiny/src/lib.rs'.in_root,
13+
produce: 'thirdparty/shiny/src/lib.rs'.in_root.as_rlib.in_build,
14+
out_dir: true,
15+
build_for: :host,
16+
}
17+
1018
# tests
1119
desc "Run tests"
1220
task :test
@@ -50,6 +58,12 @@ compile_rust :macro_ioreg, {
5058
build_for: :host,
5159
}
5260

61+
rust_tests :ioreg_test, {
62+
source: 'ioreg/test.rs'.in_root,
63+
deps: [:core_crate, :macro_ioreg, :shiny_crate],
64+
produce: 'ioreg_test'.in_build,
65+
}
66+
5367
# zinc crate
5468
compile_rust :zinc_crate, {
5569
source: 'main.rs'.in_source,
@@ -90,9 +104,10 @@ rust_tests :platformtree_test, {
90104
produce: 'platformtree_test'.in_build,
91105
}
92106

107+
# zinc test
93108
rust_tests :zinc_test, {
94109
source: 'main.rs'.in_source,
95-
deps: [:core_crate, :macro_ioreg],
110+
deps: [:core_crate, :macro_ioreg, :hamcrest_crate, :shiny_crate],
96111
produce: 'zinc_test'.in_build,
97112
recompile_on: [:platform],
98113
build_for: :host,

ioreg/builder/accessors.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn build_field_accessors<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
8484
},
8585
None => "no documentation".into_string()
8686
};
87-
let docstring = format!("*[{}]* Field `{}`: {}",
87+
let docstring = format!("*[{}]* `{}` field: {}",
8888
access_tag,
8989
field.name.node,
9090
field_doc);
@@ -113,7 +113,7 @@ fn build_get_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg)
113113
impl $reg_ty {
114114
$doc_attr
115115
#[allow(dead_code)]
116-
pub fn get(&'static self) -> $getter_ty {
116+
pub fn get(&self) -> $getter_ty {
117117
$getter_ty::new(self)
118118
}
119119
}
@@ -133,7 +133,7 @@ fn build_field_set_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
133133
if field.count.node == 1 {
134134
quote_method!(cx,
135135
#[allow(dead_code, missing_doc)]
136-
pub fn $fn_name(&'static self, new_value: $field_ty) -> $setter_ty {
136+
pub fn $fn_name<'a>(&'a self, new_value: $field_ty) -> $setter_ty<'a> {
137137
let mut setter: $setter_ty = $setter_ty::new(self);
138138
setter.$fn_name(new_value);
139139
setter
@@ -142,7 +142,7 @@ fn build_field_set_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
142142
} else {
143143
quote_method!(cx,
144144
#[allow(dead_code, missing_doc)]
145-
pub fn $fn_name(&'static self, idx: uint, new_value: $field_ty) -> $setter_ty {
145+
pub fn $fn_name<'a>(&'a self, idx: uint, new_value: $field_ty) -> $setter_ty<'a> {
146146
let mut setter: $setter_ty = $setter_ty::new(self);
147147
setter.$fn_name(idx, new_value);
148148
setter
@@ -162,14 +162,14 @@ fn build_field_get_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
162162
if field.count.node == 1 {
163163
quote_method!(cx,
164164
#[allow(dead_code, missing_doc)]
165-
pub fn $fn_name(&'static self) -> $field_ty {
165+
pub fn $fn_name(&self) -> $field_ty {
166166
$getter_ty::new(self).$fn_name()
167167
}
168168
)
169169
} else {
170170
quote_method!(cx,
171171
#[allow(dead_code, missing_doc)]
172-
pub fn $fn_name(&'static self, idx: uint) -> $field_ty {
172+
pub fn $fn_name(&self, idx: uint) -> $field_ty {
173173
$getter_ty::new(self).$fn_name(idx)
174174
}
175175
)
@@ -186,7 +186,7 @@ fn build_field_clear_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
186186
if field.count.node == 1 {
187187
quote_method!(cx,
188188
#[allow(dead_code, missing_doc)]
189-
pub fn $fn_name(&'static self) -> $setter_ty {
189+
pub fn $fn_name<'a>(&'a self) -> $setter_ty<'a> {
190190
let mut setter: $setter_ty = $setter_ty::new(self);
191191
setter.$fn_name();
192192
setter
@@ -195,7 +195,7 @@ fn build_field_clear_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
195195
} else {
196196
quote_method!(cx,
197197
#[allow(dead_code, missing_doc)]
198-
pub fn $fn_name(&'static self, idx: uint) -> $setter_ty {
198+
pub fn $fn_name<'a>(&'a self, idx: uint) -> $setter_ty<'a> {
199199
let mut setter: $setter_ty = $setter_ty::new(self);
200200
setter.$fn_name(idx);
201201
setter

ioreg/builder/getter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn build_new<'a>(cx: &'a ExtCtxt, path: &Vec<String>)
8383
utils::getter_name(cx, path));
8484
let item = quote_item!(cx,
8585
#[doc = "Create a getter reflecting the current value of the given register."]
86-
pub fn new(reg: &'static $reg_ty) -> $getter_ty {
86+
pub fn new(reg: & $reg_ty) -> $getter_ty {
8787
$getter_ty {
8888
value: reg.value.get(),
8989
}

ioreg/builder/setter.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ fn build_type<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
7575
let item = quote_item!(cx,
7676
$doc_attr
7777
#[allow(non_camel_case_types)]
78-
pub struct $name {
78+
pub struct $name<'a> {
7979
value: $packed_ty,
8080
mask: $packed_ty,
81-
reg: &'static $reg_ty,
81+
reg: &'a $reg_ty,
8282
}
8383
);
8484
item.unwrap()
@@ -92,7 +92,7 @@ fn build_new<'a>(cx: &'a ExtCtxt, path: &Vec<String>)
9292
utils::setter_name(cx, path));
9393
let item = quote_item!(cx,
9494
#[doc="Create a new updater"]
95-
pub fn new(reg: &'static $reg_ty) -> $setter_ty {
95+
pub fn new(reg: &'a $reg_ty) -> $setter_ty {
9696
$setter_ty {
9797
value: 0,
9898
mask: 0,
@@ -134,7 +134,7 @@ fn build_drop<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
134134
let item = quote_item!(cx,
135135
#[unsafe_destructor]
136136
#[doc = "This performs the register update"]
137-
impl Drop for $setter_ty {
137+
impl<'a> Drop for $setter_ty<'a> {
138138
fn drop(&mut self) {
139139
let clear_mask: $unpacked_ty = $clear as $unpacked_ty;
140140
if self.mask != 0 {
@@ -170,7 +170,7 @@ fn build_impl<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg,
170170
let done: P<ast::Method> = build_done(cx);
171171
let impl_ = quote_item!(cx,
172172
#[allow(dead_code)]
173-
impl $setter_ty {
173+
impl<'a> $setter_ty<'a> {
174174
$new
175175
$methods
176176
$done
@@ -215,8 +215,8 @@ fn build_field_set_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg,
215215
let shift = utils::shift(cx, None, field);
216216
quote_method!(cx,
217217
$doc_attr
218-
pub fn $fn_name<'a>(&'a mut self, new_value: $field_ty)
219-
-> &'a mut $setter_ty {
218+
pub fn $fn_name<'b>(&'b mut self, new_value: $field_ty)
219+
-> &'b mut $setter_ty<'a> {
220220
self.value |= (self.value & ! $mask) | ((new_value as $unpacked_ty) & $mask) << $shift;
221221
self.mask |= $mask << $shift;
222222
self
@@ -226,11 +226,11 @@ fn build_field_set_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg,
226226
let shift = utils::shift(cx, Some(quote_expr!(cx, idx)), field);
227227
quote_method!(cx,
228228
$doc_attr
229-
pub fn $fn_name<'a>(&'a mut self, idx: uint, new_value: $field_ty)
230-
-> &'a mut $setter_ty {
231-
self.value |= (self.value & ! $mask) | ((new_value as $unpacked_ty) & $mask) << $shift;
232-
self.mask |= $mask << $shift;
233-
self
229+
pub fn $fn_name<'b>(&'b mut self, idx: uint, new_value: $field_ty)
230+
-> &'b mut $setter_ty<'a> {
231+
self.value |= (self.value & ! $mask) | ((new_value as $unpacked_ty) & $mask) << $shift;
232+
self.mask |= $mask << $shift;
233+
self
234234
}
235235
)
236236
}
@@ -257,21 +257,20 @@ fn build_field_clear_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
257257
let shift = utils::shift(cx, None, field);
258258
quote_method!(cx,
259259
$doc_attr
260-
pub fn $fn_name<'a>(&'a mut self) -> &'a mut $setter_ty {
261-
self.value |= $mask << $shift;
262-
self.mask |= $mask << $shift;
263-
self
260+
pub fn $fn_name<'b>(&'b mut self) -> &'b mut $setter_ty<'a> {
261+
self.value |= $mask << $shift;
262+
self.mask |= $mask << $shift;
263+
self
264264
}
265265
)
266266
} else {
267267
let shift = utils::shift(cx, Some(quote_expr!(cx, idx)), field);
268268
quote_method!(cx,
269269
$doc_attr
270-
pub fn $fn_name<'a>(&'a mut self, idx: uint)
271-
-> &'a mut $setter_ty {
272-
self.value |= $mask << $shift;
273-
self.mask |= $mask << $shift;
274-
self
270+
pub fn $fn_name<'b>(&'b mut self, idx: uint) -> &'b mut $setter_ty<'a> {
271+
self.value |= $mask << $shift;
272+
self.mask |= $mask << $shift;
273+
self
275274
}
276275
)
277276
}

ioreg/ioreg.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,18 @@ look at `cr` in particular,
208208
209209
```
210210
impl UART_cr {
211-
pub fn get(&'static self) -> UART_cr_Get { ... }
211+
pub fn get(&self) -> UART_cr_Get { ... }
212212
213-
pub fn set_rxe(&'static self, new_value: bool) -> UART_cr_Update { ... }
214-
pub fn rxe(&'static self) -> bool { ... }
213+
pub fn set_rxe(&self, new_value: bool) -> UART_cr_Update { ... }
214+
pub fn rxe(&self) -> bool { ... }
215215
216216
// similar methods for `txe`, `rxie`, `txie`
217217
218-
pub fn set_br(&'static self, new_value: u32) -> UART_cr_Update { ... }
219-
pub fn br(&'static self) -> u32 { ... }
218+
pub fn set_br(&self, new_value: u32) -> UART_cr_Update { ... }
219+
pub fn br(&self) -> u32 { ... }
220220
221-
pub fn set_parity(&'static self, new_value: UART_cr_parity) -> UART_cr_Update { ... }
222-
pub fn parity(&'static self) -> UART_cr_parity { ... }
221+
pub fn set_parity(&self, new_value: UART_cr_parity) -> UART_cr_Update { ... }
222+
pub fn parity(&self) -> UART_cr_parity { ... }
223223
}
224224
```
225225
@@ -264,7 +264,7 @@ method is instead produced. For instance, in the case of the `sr`
264264
register's `fe` flag,
265265
266266
```
267-
pub fn clear_fe(&'static self) -> UART_sr_Update { ... }
267+
pub fn clear_fe(&self) -> UART_sr_Update { ... }
268268
```
269269
270270
### Informal grammar

ioreg/test.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Zinc, the bare metal stack for rust.
2+
// Copyright 2014 Ben Gamari <[email protected]>
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
//! Tests for ioreg! syntax extension
17+
18+
#![feature(phase)]
19+
#[phase(plugin)] extern crate macro_ioreg;
20+
#[phase(plugin,link)] extern crate shiny;
21+
extern crate core;
22+
23+
24+
#[allow(dead_code)]
25+
#[path="../src/lib/volatile_cell.rs"] mod volatile_cell;
26+
27+
#[cfg(test)]
28+
mod test {
29+
use std::mem::{transmute, zeroed};
30+
use std::ptr::RawPtr;
31+
use volatile_cell::VolatileCell;
32+
33+
fn get_value<'a, T>(v: &'a T, offset: uint) -> u32 {
34+
unsafe {
35+
let ptr: *const u32 = transmute(v);
36+
*(ptr.offset(offset as int))
37+
}
38+
}
39+
40+
fn zeroed_safe<T: Copy>() -> T {
41+
unsafe {
42+
return zeroed();
43+
}
44+
}
45+
46+
ioregs!(BASIC_TEST = {
47+
0x0 => reg32 reg1 {
48+
0 => field1,
49+
1..3 => field2,
50+
16..24 => field3,
51+
25 => field4: set_to_clear,
52+
}
53+
0x4 => reg32 reg2 {
54+
0 => field1,
55+
}
56+
})
57+
58+
describe!(
59+
before_each {
60+
let test: BASIC_TEST = zeroed_safe();
61+
}
62+
63+
it "can round_trip simple field values (1)" {
64+
test.reg1.set_field1(true);
65+
assert_eq!(test.reg1.field1(), true)
66+
assert_eq!(get_value(&test, 0), 1)
67+
assert_eq!(get_value(&test, 1), 0)
68+
}
69+
70+
it "can round trip simple field values (2)" {
71+
test.reg1.set_field3(0xde);
72+
assert_eq!(test.reg1.field3(), 0xde)
73+
assert_eq!(get_value(&test, 0), 0xde<<16)
74+
}
75+
76+
it "sets set_to_clear fields" {
77+
test.reg1.clear_field4();
78+
assert_eq!(get_value(&test, 0), 1<<25)
79+
}
80+
)
81+
82+
ioregs!(GROUP_TEST = {
83+
0x0 => group regs[5] {
84+
0x0 => reg32 reg1 {
85+
0..31 => field1
86+
}
87+
0x4 => reg32 reg2 {
88+
0..31 => field2
89+
}
90+
}
91+
})
92+
93+
describe!(
94+
before_each {
95+
let test: GROUP_TEST = zeroed_safe();
96+
}
97+
98+
it "sets groups correctly" {
99+
test.regs[0].reg1.set_field1(0xdeadbeef);
100+
assert_eq!(test.regs[0].reg1.field1(), 0xdeadbeef)
101+
assert_eq!(get_value(&test, 0), 0xdeadbeef)
102+
for i in range(1, 10) {
103+
assert_eq!(get_value(&test, i), 0)
104+
}
105+
106+
test.regs[2].reg2.set_field2(0xfeedbeef);
107+
assert_eq!(test.regs[2].reg2.field2(), 0xfeedbeef)
108+
assert_eq!(get_value(&test, 5), 0xfeedbeef)
109+
}
110+
)
111+
112+
ioregs!(FIELD_ARRAY_TEST = {
113+
0x0 => reg32 reg1 {
114+
0..31 => field[16]
115+
}
116+
})
117+
118+
describe!(
119+
before_each {
120+
let test: FIELD_ARRAY_TEST = zeroed_safe();
121+
}
122+
123+
it "sets field arrays correctly" {
124+
test.reg1.set_field(0, 1);
125+
assert_eq!(test.reg1.field(0), 1);
126+
assert_eq!(get_value(&test, 0), 0x1)
127+
128+
test.reg1.set_field(4, 3);
129+
assert_eq!(test.reg1.field(4), 3);
130+
assert_eq!(get_value(&test, 0), 0x1 | 0x3<<8)
131+
}
132+
)
133+
}

support/rake.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,8 @@ def provide_stdlibs
162162
Rake::FileTask.define_task 'thirdparty/hamcrest-rust'.in_root do |t|
163163
sh "git clone --single-branch --depth 1 https://github.com/carllerche/hamcrest-rust #{t.name}"
164164
end.invoke
165+
166+
Rake::FileTask.define_task 'thirdparty/shiny'.in_root do |t|
167+
sh "git clone --single-branch --depth 1 https://github.com/farcaller/shiny #{t.name}"
168+
end.invoke
165169
end

0 commit comments

Comments
 (0)