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

Commit dd11314

Browse files
committed
Merge pull request #128 from bgamari/ioreg-test
Add rudimentary tests for ioreg Reviewed-by:
2 parents 2af9fe6 + 11b6e7a commit dd11314

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
@@ -82,7 +82,7 @@ fn build_field_accessors<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
8282
},
8383
None => "no documentation".into_string()
8484
};
85-
let docstring = format!("*[{}]* Field `{}`: {}",
85+
let docstring = format!("*[{}]* `{}` field: {}",
8686
access_tag,
8787
field.name.node,
8888
field_doc);
@@ -111,7 +111,7 @@ fn build_get_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg)
111111
impl $reg_ty {
112112
$doc_attr
113113
#[allow(dead_code)]
114-
pub fn get(&'static self) -> $getter_ty {
114+
pub fn get(&self) -> $getter_ty {
115115
$getter_ty::new(self)
116116
}
117117
}
@@ -131,7 +131,7 @@ fn build_field_set_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
131131
if field.count.node == 1 {
132132
quote_method!(cx,
133133
#[allow(dead_code, missing_doc)]
134-
pub fn $fn_name(&'static self, new_value: $field_ty) -> $setter_ty {
134+
pub fn $fn_name<'a>(&'a self, new_value: $field_ty) -> $setter_ty<'a> {
135135
let mut setter: $setter_ty = $setter_ty::new(self);
136136
setter.$fn_name(new_value);
137137
setter
@@ -140,7 +140,7 @@ fn build_field_set_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
140140
} else {
141141
quote_method!(cx,
142142
#[allow(dead_code, missing_doc)]
143-
pub fn $fn_name(&'static self, idx: uint, new_value: $field_ty) -> $setter_ty {
143+
pub fn $fn_name<'a>(&'a self, idx: uint, new_value: $field_ty) -> $setter_ty<'a> {
144144
let mut setter: $setter_ty = $setter_ty::new(self);
145145
setter.$fn_name(idx, new_value);
146146
setter
@@ -160,14 +160,14 @@ fn build_field_get_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
160160
if field.count.node == 1 {
161161
quote_method!(cx,
162162
#[allow(dead_code, missing_doc)]
163-
pub fn $fn_name(&'static self) -> $field_ty {
163+
pub fn $fn_name(&self) -> $field_ty {
164164
$getter_ty::new(self).$fn_name()
165165
}
166166
)
167167
} else {
168168
quote_method!(cx,
169169
#[allow(dead_code, missing_doc)]
170-
pub fn $fn_name(&'static self, idx: uint) -> $field_ty {
170+
pub fn $fn_name(&self, idx: uint) -> $field_ty {
171171
$getter_ty::new(self).$fn_name(idx)
172172
}
173173
)
@@ -184,7 +184,7 @@ fn build_field_clear_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
184184
if field.count.node == 1 {
185185
quote_method!(cx,
186186
#[allow(dead_code, missing_doc)]
187-
pub fn $fn_name(&'static self) -> $setter_ty {
187+
pub fn $fn_name<'a>(&'a self) -> $setter_ty<'a> {
188188
let mut setter: $setter_ty = $setter_ty::new(self);
189189
setter.$fn_name();
190190
setter
@@ -193,7 +193,7 @@ fn build_field_clear_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
193193
} else {
194194
quote_method!(cx,
195195
#[allow(dead_code, missing_doc)]
196-
pub fn $fn_name(&'static self, idx: uint) -> $setter_ty {
196+
pub fn $fn_name<'a>(&'a self, idx: uint) -> $setter_ty<'a> {
197197
let mut setter: $setter_ty = $setter_ty::new(self);
198198
setter.$fn_name(idx);
199199
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,
@@ -125,7 +125,7 @@ fn build_drop<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
125125
let item = quote_item!(cx,
126126
#[unsafe_destructor]
127127
#[doc = "This performs the register update"]
128-
impl Drop for $setter_ty {
128+
impl<'a> Drop for $setter_ty<'a> {
129129
fn drop(&mut self) {
130130
let clear_mask: $unpacked_ty = $clear as $unpacked_ty;
131131
if self.mask != 0 {
@@ -161,7 +161,7 @@ fn build_impl<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg,
161161
let done: P<ast::Method> = build_done(cx);
162162
let impl_ = quote_item!(cx,
163163
#[allow(dead_code)]
164-
impl $setter_ty {
164+
impl<'a> $setter_ty<'a> {
165165
$new
166166
$methods
167167
$done
@@ -206,8 +206,8 @@ fn build_field_set_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg,
206206
let shift = utils::shift(cx, None, field);
207207
quote_method!(cx,
208208
$doc_attr
209-
pub fn $fn_name<'a>(&'a mut self, new_value: $field_ty)
210-
-> &'a mut $setter_ty {
209+
pub fn $fn_name<'b>(&'b mut self, new_value: $field_ty)
210+
-> &'b mut $setter_ty<'a> {
211211
self.value |= (self.value & ! $mask) | ((new_value as $unpacked_ty) & $mask) << $shift;
212212
self.mask |= $mask << $shift;
213213
self
@@ -217,11 +217,11 @@ fn build_field_set_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg,
217217
let shift = utils::shift(cx, Some(quote_expr!(cx, idx)), field);
218218
quote_method!(cx,
219219
$doc_attr
220-
pub fn $fn_name<'a>(&'a mut self, idx: uint, new_value: $field_ty)
221-
-> &'a mut $setter_ty {
222-
self.value |= (self.value & ! $mask) | ((new_value as $unpacked_ty) & $mask) << $shift;
223-
self.mask |= $mask << $shift;
224-
self
220+
pub fn $fn_name<'b>(&'b mut self, idx: uint, new_value: $field_ty)
221+
-> &'b mut $setter_ty<'a> {
222+
self.value |= (self.value & ! $mask) | ((new_value as $unpacked_ty) & $mask) << $shift;
223+
self.mask |= $mask << $shift;
224+
self
225225
}
226226
)
227227
}
@@ -248,21 +248,20 @@ fn build_field_clear_fn<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
248248
let shift = utils::shift(cx, None, field);
249249
quote_method!(cx,
250250
$doc_attr
251-
pub fn $fn_name<'a>(&'a mut self) -> &'a mut $setter_ty {
252-
self.value |= $mask << $shift;
253-
self.mask |= $mask << $shift;
254-
self
251+
pub fn $fn_name<'b>(&'b mut self) -> &'b mut $setter_ty<'a> {
252+
self.value |= $mask << $shift;
253+
self.mask |= $mask << $shift;
254+
self
255255
}
256256
)
257257
} else {
258258
let shift = utils::shift(cx, Some(quote_expr!(cx, idx)), field);
259259
quote_method!(cx,
260260
$doc_attr
261-
pub fn $fn_name<'a>(&'a mut self, idx: uint)
262-
-> &'a mut $setter_ty {
263-
self.value |= $mask << $shift;
264-
self.mask |= $mask << $shift;
265-
self
261+
pub fn $fn_name<'b>(&'b mut self, idx: uint) -> &'b mut $setter_ty<'a> {
262+
self.value |= $mask << $shift;
263+
self.mask |= $mask << $shift;
264+
self
266265
}
267266
)
268267
}

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)