Macs have an "Apple hardware test" you can boot into. And you could as well just boot from an external drive to run all kinds of diagnostics.
Phones however...
Phones however...
https://www.ifixit.com/News/33593/heres-the-secret-repair-to...
In principle, it should be possible to have a diagnostic mode the same way recovery is implemented. But there are no device I know of that have this.