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