Repository navigation
Conversation
Part of RT-Thread#11972. ADC had neither a utest nor a simulated device, so the framework was never exercised in CI. The testcase registers its own simulated rt_adc_device, so no hardware and no QEMU peripheral model is required, and covers: adc_tc_register_test device class, ops pointer, name lookup adc_tc_enable_disable_test enabled() receives the right channel and flag adc_tc_read_test rt_adc_read() returns the raw value, and 0 when convert() fails instead of a stale value adc_tc_voltage_test scaling by resolution and vref, plus exactly one enable / convert / disable per call adc_tc_missing_hooks_test -RT_ENOSYS when the optional enabled() hook is absent The expected millivolt value is hardcoded rather than recomputed in the test: recomputing it with the same formula as the code under test would keep passing even when the formula is wrong. Verified by injecting a deliberate error (/ ((1 << res) - 2) instead of - 1): with raw = 2048 that mutation is invisible because both give 1650, so the case uses raw = 1000, where 805 and 806 differ. With the mutation in place the test fails as intended: [E/utest] at (adc_tc.c); func: (adc_tc_voltage_test:201); msg: ((rt_adc_voltage(dev, 3)) not == (805)) Verified on bsp/qemu-vexpress-a9 with RT_UTEST_ADC + RT_UTEST_USING_AUTO_RUN: [I/utest] utest unit name: (adc_tc_register_test) ... [I/utest] utest unit name: (adc_tc_missing_hooks_test) [I/utest] [ PASSED ] testcase (components.drivers.adc.framework) adc/SConscript gains the usual sub-directory traversal so that utest/ is picked up, matching what rtc/ and spi/ already do. Refs RT-Thread#11972
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-11-02 16:09 CST)
📝 Review Instructions
|
|
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
#11972 的清单里 ADC 是 utest 与 QEMU 双空的一项。按 issue 描述的思路("注册好虚拟的 ops,仿造一个模拟设备,把接口入参都测试完整"),先补 utest 这一半——本 PR 不需要任何硬件,也不需要 QEMU 外设模型。
你的解决方案是什么 (what is your solution)
新增
components/drivers/adc/utest/adc_tc.c,用例内注册一个模拟rt_adc_device,覆盖五个单元:adc_tc_register_testRT_Device_Class_ADC、ops 指针、按名字查找adc_tc_enable_disable_testenabled()收到的 channel 与标志正确,次数可控adc_tc_read_testrt_adc_read()返回原始值;convert()失败时返回 0 而不是旧值adc_tc_voltage_testadc_tc_missing_hooks_testenabled()时返回-RT_ENOSYS配置项为
RT_UTEST_ADC(depends on RT_USING_ADC),同时给adc/SConscript补上子目录遍历,与rtc/、spi/的既有写法一致。关于断言设计的一个坑
期望毫伏值写死为常量,不在测试里按同一公式重算:
最初我是用
value * vref / ((1 << resolution) - 1)在测试里自己算期望值的,结果故意把被测公式改错(- 1改成- 2)后测试依然通过——因为两边用同一个错的公式,一起错。这一点值得强调:测不出错误的用例等于没写。改成写死常量后,还发现取值也有讲究:
raw = 2048时/4095和/4094恰好都得 1650,注入错误仍然过;换成raw = 1000才有区分(805 vs 806)。请提供验证的bsp和config (provide the config and bsp)
bsp/qemu-vexpress-a9RT_USING_ADC=y(走RT_USING_ADC_V1)、RT_UTEST_ADC=y、RT_UTEST_USING_AUTO_RUN=y1c801efa正常构建:
注入错误(把除法分母改成
- 2)后,如预期失败:尚未覆盖
rt_adc_voltage()里get_resolution/get_vref/enabled是直接调用、没有 NULL 判断(对比rt_adc_enable()有判断)。驱动若只实现convert,这三个回调缺失就会空指针。我在adc_tc_missing_hooks_test里只覆盖了rt_adc_enable()/disable()的-RT_ENOSYS路径,没有去调用rt_adc_voltage(),因为那会崩溃。这个缺口是否要单独修,请维护者判断。]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up