1 // Copyright 2018 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 use quote::quote;
6 use syn::parse_quote;
7 use syn::DeriveInput;
8
9 #[test]
test_variant_bits()10 fn test_variant_bits() {
11 let mut variants = vec![parse_quote!(A)];
12 assert_eq!(crate::variant_bits(&variants), 0);
13
14 variants.push(parse_quote!(B));
15 variants.push(parse_quote!(C));
16 assert_eq!(crate::variant_bits(&variants), 2);
17
18 for _ in 0..1021 {
19 variants.push(parse_quote!(Dynamic));
20 }
21 assert_eq!(crate::variant_bits(&variants), 10);
22
23 variants.push(parse_quote!(OneMore));
24 assert_eq!(crate::variant_bits(&variants), 11);
25 }
26
27 #[test]
event_token_e2e()28 fn event_token_e2e() {
29 let input: DeriveInput = parse_quote! {
30 enum Token {
31 A,
32 B,
33 C,
34 D(usize),
35 E { foobaz: u32 },
36 }
37 };
38
39 let actual = crate::event_token_inner(input);
40 let expected = quote! {
41 impl EventToken for Token {
42 fn as_raw_token(&self) -> u64 {
43 match *self {
44 Token::A => 0u64,
45 Token::B => 1u64,
46 Token::C => 2u64,
47 Token::D { 0: data } => 3u64 | ((data as u64) << 3u32),
48 Token::E { foobaz: data } => 4u64 | ((data as u64) << 3u32),
49 }
50 }
51
52 fn from_raw_token(data: u64) -> Self {
53 match data & 7u64 {
54 0u64 => Token::A,
55 1u64 => Token::B,
56 2u64 => Token::C,
57 3u64 => Token::D { 0: (data >> 3u32) as usize },
58 4u64 => Token::E { foobaz: (data >> 3u32) as u32 },
59 _ => unreachable!(),
60 }
61 }
62 }
63 };
64
65 assert_eq!(actual.to_string(), expected.to_string());
66 }
67